使用php和ajax发送数据后执行curl函数
我有一个表单,有5个字段和一个锚,比如submit buton和ajax。根据表单中插入的内容,我需要执行一个函数来检索一些消息。此功能根据电话号码和代码从其他主机(链接)检索不同的消息。当我使用php和ajax将数据插入数据库以显示来自该主机的答案(链接)时,如何使用此函数 这是我的密码: 验证代码的函数:使用php和ajax发送数据后执行curl函数,php,ajax,wordpress,curl,Php,Ajax,Wordpress,Curl,我有一个表单,有5个字段和一个锚,比如submit buton和ajax。根据表单中插入的内容,我需要执行一个函数来检索一些消息。此功能根据电话号码和代码从其他主机(链接)检索不同的消息。当我使用php和ajax将数据插入数据库以显示来自该主机的答案(链接)时,如何使用此函数 这是我的密码: 验证代码的函数: 功能点击检查代码新($phone,$code){ $url='mycustom_path_not_displated_for_security_reasons/filejson.php';
功能点击检查代码新($phone,$code){
$url='mycustom_path_not_displated_for_security_reasons/filejson.php';
$fields=数组(
'phone'=>urlencode($phone),
'code'=>urlencode($code)
);
//url为帖子提供数据
foreach($key=>$value形式的字段){$fields_string.=$key.'='.$value.&';}
rtrim($fields_string,&');
//开放连接
$ch=curl_init();
//设置url、POST变量数和POST数据
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);
//执行职务
$result=curl\u exec($ch);
//密切联系
卷曲关闭($ch);
$what_i_received_from_testmail=json_decode($result,true);
if(!isset($what_i_received_from_testmail['Respond'])返回“Invalid response!”;
if($what_i_received_from_testmail['Respond']=='ACCESDENIED')返回'Error:IP invalid!';
if($what_i_received_from_testmail['Respond']=='INVALIDPARAMS')返回'Error:invalid input params';
if($what_i_received_from_testmail['response']='TIMEOUT')返回“错误:SQL没有响应!”;
返回$what_i_received_from_testmail['Respond'];
}
add_action(“wp_ajax_输入_代码”、“输入_代码”);
添加动作(“wp\u ajax\u nopriv\u输入代码”、“输入代码”);
函数输入_代码(){
全球$wpdb;
$code=(isset($\u POST['code'])?htmlspecialchars(trim($\u POST['code']):“”;
$fname=(isset($_POST['fname'])?htmlspecialchars(trim($_POST['fname']):“”;
$lname=(isset($_POST['lname'])?htmlspecialchars(trim($_POST['lname']):“”;
$phone=(isset($\u POST['phone'])?htmlspecialchars(trim($\u POST['phone']):“”;
$email=(isset($_POST['email'])?htmlspecialchars(trim($_POST['email']):“”;
if(设置($\u POST['code'])&&&!空($\u POST)&&is\u user\u logged\u in()){
$table='tst_竞赛';
$data=数组(
“代码”=>$code,
“fname”=>$fname,
“lname”=>$lname,
'phone'=>$phone,
'email'=>$email
);
$format=数组('%s','%s','%s','%s','%s','%s');
$success=$wpdb->insert($table,$data,$format);
}
}
我还有一个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();
}
我的最后一个问题是:如何将第一个函数实现到第二个函数中,以获得我在开始时所说的内容?提前感谢
第二个ajax,正如Fresh建议的那样(我在第一个ajax成功函数中使用了这个):
$.ajax({
类型:“POST”,
url:ajaxurl,
数据:{操作:'hit_check_code_new',电话:phone,code:code},
成功:功能(msg){
log(“第二个AJAX请求”);
}
})
请参阅代码片段,它将以某种方式帮助您解决问题
<?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');
在ajax响应中调用另一个ajax请求,并指出带有参数的函数。请给我一个例子?!为什么首先要从ajax获得响应?在
$success
变量之后,您可以直接调用点击检查\u code\u new($phone,$code)
?我试过了。它不起作用,我也不知道为什么。我希望在执行ajax之后使用curl函数,并且我已经用您的第一个建议更新了我的问题。这就是你说的?我已经把代码放在问题的末尾了。请查收。我在第一个ajax的success函数中使用了该ajax。