使用php和ajax发送数据后执行curl函数

时间:2015-08-13 12:08:20

标签: php ajax wordpress curl

我有一个包含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");
				}
	})

1 个答案:

答案 0 :(得分:0)

请参阅代码段以帮助您以某种方式解决

<?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函数,而是做一些事情,因为我表示为最后一个函数。

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();

}