【问题标题】:Execute curl function after sending data with php and ajax用php和ajax发送数据后执行curl函数
【发布时间】:2015-11-06 09:31:30
【问题描述】:

我有一个带有 5 个字段的表单和一个锚点作为带有 ajax 的提交按钮。我需要执行一个函数来检索一些消息,具体取决于表单中插入的内容。此函数根据电话号码和代码从另一个主机(链接)检索不同的消息。当我使用 php 和 ajax 将数据插入数据库以显示来自该主机(链接)的答案时,如何使用此功能?

这是我的代码:

验证代码的函数:

function hit_check_code_new($phone, $code){
    $url = 'mycustom_path_not_displayed_for_security_reasons/filejson.php';
    $fields = array(
            'phone' => urlencode($phone),
            'code' => urlencode($code)
    );

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //execute post
    $result = curl_exec($ch);
    //close connection
    curl_close($ch);

    $what_i_received_from_testmail = json_decode($result,true);   
    if (!isset($what_i_received_from_testmail['Respond'])) return 'Invalid response!';
    if ($what_i_received_from_testmail['Respond'] == 'ACCESDENIED') return 'Error: IP invalid!';
    if ($what_i_received_from_testmail['Respond'] == 'INVALIDPARAMS') return 'Error: invalid input params';
    if ($what_i_received_from_testmail['Respond'] == 'TIMEOUT') return 'Error: SQL is not responding!';
    return $what_i_received_from_testmail['Respond'];
}
add_action("wp_ajax_enter_code", "enter_code");
add_action("wp_ajax_nopriv_enter_code", "enter_code");
function enter_code(){
    global $wpdb;


    $code = (isset($_POST['code']))?htmlspecialchars(trim($_POST['code'])):'';
    $fname = (isset($_POST['fname']))?htmlspecialchars(trim($_POST['fname'])):'';
    $lname = (isset($_POST['lname']))?htmlspecialchars(trim($_POST['lname'])):'';
    $phone = (isset($_POST['phone']))?htmlspecialchars(trim($_POST['phone'])):'';
    $email = (isset($_POST['email']))?htmlspecialchars(trim($_POST['email'])):'';


    if(isset($_POST['code']) && !empty($_POST) && is_user_logged_in()){
        $table = 'tst_contest';
        $data = array(
                'code' => $code,
                'fname'         => $fname,
                'lname'           => $lname,
                'phone'           => $phone,
                'email'           => $email 
        );
        $format = array('%s', '%s', '%s', '%s', '%s');
        $success = $wpdb->insert( $table, $data, $format );
    }
}

我还有一个 ajax 代码,可以很好地通过最后一个函数将数据插入数据库。

我的最后一个问题是:如何将第一个函数实现到第二个函数中以获得我开头所说的内容?提前致谢。

Fresher建议的第二个 ajax(我在第一个 ajax succes 函数中使用了这个):

$.ajax({
		type: "POST",
		url: ajaxurl,
		data: {action: 'hit_check_code_new', phone: phone, code: code},
		success: function(msg){
			console.log("SECOND AJAX REQUEST");
				}
	})

【问题讨论】:

  • 在 ajax 响应中调用另一个 ajax 请求并用你的参数指出函数
  • 你能给我举个例子吗?!
  • 为什么要首先从 ajax 获得响应?你可以在$success变量之后直接调用hit_check_code_new($phone, $code)吗?
  • 我试过了。它没有用,我不知道为什么。我想在执行 ajax 后使用 curl 函数。
  • 我也用你的第一个建议更新了我的问题。你指的是那个?我已将代码放在问题的末尾。请检查。我在第一个 ajax 的 succes 函数中使用了那个 ajax。

标签: php ajax wordpress curl


【解决方案1】:

看sn-p会帮你解决

<?php
/*
 * Plugin Name: Testing
 *
 */

add_action('wp_ajax_hit_check_code_new', 'curl_response');
add_action('wp_ajax_nopriv_hit_check_code_new', 'curl_response');

function curl_response() {
    $ch = curl_init();
    $ajax_url = 'http://localhost/fantastic/wp-admin/admin-ajax.php';

    curl_setopt($ch, CURLOPT_URL, $ajax_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('action' => 'ajaxTester', 'param1' => $_POST['phone'])));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
    exit();
}

add_action('wp_head', 'button_ajax');

function button_ajax() {
    ?>
    <input type="text" name="fresher_text_field" class="fresher_text_field" value="124578989"/>
    <input type="submit" name="fresher_click_me" id="fresher_click_me" value="Submit Me" />
    <script type="text/javascript">
        jQuery(function () {
            jQuery('#fresher_click_me').click(function () {
                jQuery.ajax({
                    type: "POST",
                    url: "<?php echo admin_url('admin-ajax.php'); ?>",
                    data: {action: 'hit_check_code_new', phone: jQuery('.fresher_text_field').val()},
                    success: function (msg) {
                        console.log(msg);
                    }
                });
            });
        });

    </script>
    <?php
}

function handle_ajaxTester() {
    if (isset($_POST['param1']) && $_POST['param1'] != '') {
        echo '<br>Response:<br>';
        print_r($_POST);
        exit();
    }
}

add_action('wp_ajax_ajaxTester', 'handle_ajaxTester');
add_action('wp_ajax_nopriv_ajaxTester', 'handle_ajaxTester');

编辑:

请不要直接在 curl 代码中指向 ajax 函数,而是按照 i 表示的最后一个函数进行操作。

function hit_check_code_new($phone, $code){
 // Your Code
}

function second_ajax_function() {
 $phone = $_POST['phone'];
 $code = $_POST['code'];
 echo hit_check_code_new($phone,$code);
 exit();

}

【讨论】:

    猜你喜欢
    • 2013-04-08
    • 2010-11-08
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    相关资源
    最近更新 更多