php - 如何通过cron php在API中插入数据
问题描述
嗨,我想编写代码以在 MySQL 表中插入数据,并连同它一起插入到 API。
我能够在 MySQL 数据库中插入数据,但是当我编写在 API 中推送数据的代码时,它也不会在 MySQL 表中插入数据。
$fname = $value['first_name'].' '.$value['last_name'];
$phone = $value["phone_number"];
$email = $value["email"];
$city = addslashes($value["city"]);
$client_source = "Home Visit";
$source = "Facebook Home Visit";
$z_uid=$value["z_uid"];
$query1="INSERT INTO amplifon_web_leads(submitted,enquiry_id,yourname,email,phone,preferred_centre,status,region_id,brand_id,source,user_id,lead_handler_name,Priority,client_source) VALUES(date_add(now(),INTERVAL 630 Minute),CONCAT('AMP','$enquiry_code'),'$fname','$email','$phone','$city','16001','1601','16','$source','$user_id','$handler_name','','$client_source')";
mysqli_query($connection,$query1) or die ('Error updating database1');
$query2="DELETE FROM `amplifon_facebook_zipper` WHERE `z_uid`='$z_uid'";
mysqli_query($connection,$query2) or die ('Error updating database2');
$url = "http://APIURL.COM/Campaign/API/Intekill/Enquiry";
$enquiry_codes = CONCAT('AMP','$enquiry_code');
$data = array("Name" => $fname, "InterSkaleEnquiryLeadId" => $enquiry_codes,"Mobile"=>$phone,"Phone"=>$phone,"Email"=>$email,"CenterType"=>$city,"PrefCenter"=>$city, "Enquiry"=> "from cron", "OtherCenter"=> "null", "Pname"=>"null", "Pmobile"=>$phone, "CallDate"=> "null", "CallTime"=> "null","ClientSource"=> "null" );
$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array( "Enquiry"=> $data ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);
我想在 API 和 MySQL 表中插入数据。
解决方案
嘿伙计们,感谢您的评论,现在它可以使用以下代码,我犯了两个错误。
我使用 concat() 函数会产生错误
我将内容类型更改为编码并且它有效。
$fname = $value['first_name'].' '.$value['last_name']; $phone = $value["phone_number"]; $email = $value["email"]; $city = addslashes($value["city"]); $client_source = "Home Visit"; $source = "Facebook Home Visit"; $z_uid=$value["z_uid"]; $query1="INSERT INTO amplifon_web_leads(submitted,enquiry_id,yourname,email,phone,preferred_centre,status,region_id,brand_id,source,user_id,lead_handler_name,Priority,client_source) VALUES(date_add(now(),INTERVAL 630 Minute),CONCAT('AMP','$enquiry_code'),'$fname','$email','$phone','$city','16001','1601','16','$source','$user_id','$handler_name','','$client_source')"; mysqli_query($connection,$query1) or die ('Error updating database1'); $query2="DELETE FROM `amplifon_facebook_zipper` WHERE `z_uid`='$z_uid'"; mysqli_query($connection,$query2) or die ('Error updating database2'); $enquiry_codes = 'AMP'.'$enquiry_code'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://ipaddress/Campaign/API/InterSkill/Enquiry"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,"Name=".$fname."&InterSkaleEnquiryLeadId=".$enquiry_codes."&Mobile=".$phone."&Phone=".$phone."&Email=".$email."&CenterType=".$city."&PrefCenter=".$city."&Enquiry=FROMcron&OtherCenter=null&Pname=null&Pmobile=null&CallDate=null&CallTime=null&ClientSource=null"); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); // receive server response ... curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch);
推荐阅读
- android - 是否可以在 Android 10 及更高版本上从路径(外部存储)将图像设置为 ImageView?
- c# - C# form.show() 不在计时器中运行
- node.js - 如何使用 ESlint 修复我的 Node.js 语法?
- javascript - 弹出窗口关闭时如何重置javascript游戏
- android - Android:将从前台服务接收到的数据显示到活动内的 MapView 上
- javascript - 如何在 AGGrid 初始化后更改 floatingFilter 选项?
- wso2 - WSO2 身份服务器 - 禁用不区分大小写的用户名
- video - 如何将视频存储在 Ant Media Server 中的另一个驱动器中?
- json - Kotlin 中的 Jackson @JsonView 抛出 MissingKotlinParameterException
- google-apps-script - 使用 Google 趋势系统地比较关键字