Php 在无卷曲的循环中提交POST数据并获取响应数据-CodeIgniter
我有一个向客户发送短信的代码,该代码被设置为cron作业。我的问题是,当我使用cURL时,它会发送两部分信息,我们的钱包会被收取两次费用。我们平均每天要发送500条消息。我的目标是使信息只包含一部分 所以我试图找到在循环中发送消息而不使用cURL的最佳方法。 我曾想过将循环中的post数据保存在一个数组中,并将其发送到我的视图中。然后在调用视图时,我将自动发送表单,而无需点击submit按钮并使用javascript自动提交。调用视图后,我将使用file_get_contents()从URL获取响应。但我不能让它工作:Php 在无卷曲的循环中提交POST数据并获取响应数据-CodeIgniter,php,api,codeigniter,codeigniter-3,sms-gateway,Php,Api,Codeigniter,Codeigniter 3,Sms Gateway,我有一个向客户发送短信的代码,该代码被设置为cron作业。我的问题是,当我使用cURL时,它会发送两部分信息,我们的钱包会被收取两次费用。我们平均每天要发送500条消息。我的目标是使信息只包含一部分 所以我试图找到在循环中发送消息而不使用cURL的最佳方法。 我曾想过将循环中的post数据保存在一个数组中,并将其发送到我的视图中。然后在调用视图时,我将自动发送表单,而无需点击submit按钮并使用javascript自动提交。调用视图后,我将使用file_get_contents()从URL获取
public function send_sms_globe(){
error_reporting(-1);
ini_set('display_errors', 1);
set_time_limit(0);
//get all data from the database pull with status = queue
$globe_data = $this->New_Sms_Api_model->get_queued_data('globe_api');
$passphrase = '[our_pass_phrase]';
$app_id = '[our_app_id]';
$app_secret = '[our_app_secret]';
$url = 'https://devapi.globelabs.com.ph/smsmessaging/v1/outbound/<our_shortcode>/requests/';
$ch = curl_init($url);
$limit = 0;
foreach($globe_data AS $records_data){
if($limit == 49){
break;
}
switch($limit) {
case 49:
$limit = 0;
break;
default:
if($records_data['Remarks'] == 'LOADED'){
if($records_data['sent_to'] == 'sender'){
$address = $records_data['sender_phone_number'];
}else if($records_data['sent_to'] == 'consignee'){
$address = $records_data['consignee_phone_number'];
}
} else {
$address = $records_data['sender_phone_number'];
}
//$address = '+63917*******';//address : *subscriber number $records_data['phone_number'];
$message = (isset($records_data['Message']) && $records_data['Message'] != '') ? $records_data['Message']:''; //message : *sms content
$str = iconv('UTF-8', 'ASCII//IGNORE//TRANSLIT', $message);
$post_data = [
'app_id' => $app_id,
'app_secret' => $app_secret,
'passphrase' => $passphrase,
'message' => rawurlencode($str),
'address' => $address
];
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// execute!
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$queue_id = $records_data['ID'];
$invoice_number = $records_data['InvoicecNumber'];
$status_remark = $records_data['Remarks'];
$record_id = $records_data['record_id'];
if($http_code == 201 || $http_code == 200){
$no_of_tries = $records_data['no_of_tries'];
if($no_of_tries == 0){
$no_of_tries = 1;
} else {
$no_of_tries = $records_data['no_of_tries'];
}
$status = 'sent';
$retry = 0;
} else {
$no_of_tries = $records_data['no_of_tries'] + 1;
if($no_of_tries == 3){
$status = 'failed';
$retry = 0;
} else {
$status = 'retry';
$retry = 1;
}
}
$update_queued_data = $this->New_Sms_Api_model->update_queued_data($queue_id, $invoice_number, $status, $retry, $no_of_tries);
if($update_queued_data){
if($status == 'sent'){
if($status_remark == 'LOADED'){
$sent_to = $records_data['sent_to'];
} else {
$sent_to = NULL;
}
$this->New_Sms_Api_model->save_to_cq_sms($invoice_number, $status_remark, $record_id,$sent_to);
echo $records_data['record_id'].' ---- '.$status;
}
$limit++;
}
}
}
// close the connection, release resources used
curl_close($ch);
}
公共功能发送短信地球仪(){
错误报告(-1);
ini设置(“显示错误”,1);
设置时间限制(0);
//从数据库获取状态为队列的所有数据
$globe_data=$this->New_Sms_Api_model->get_queued_data('globe_Api');
$passphrase='[我们的通行短语];
$app_id='[我们的app_id]';
$app_secret='[我们的app_secret]';
$url='1https://devapi.globelabs.com.ph/smsmessaging/v1/outbound//requests/';
$ch=curl\u init($url);
$limit=0;
foreach($globe\u数据作为$records\u数据){
如果($limit==49){
打破
}
开关($限制){
案例49:
$limit=0;
打破
违约:
如果($records_data['Comments']=='LOADED'){
如果($records_data['Send_to']=='sender'){
$address=$records_data['sender_phone_number'];
}else if($records_data['send_to']=='tacker'){
$address=$records_data[‘收货人电话号码’];
}
}否则{
$address=$records_data['sender_phone_number'];
}
//$address='+63917******';//地址:*订户号$records_data['phone_number'];
$message=(isset($records\u data['message'])和&$records\u data['message']!=''?$records\u data['message']:'';//短信:*短信内容
$str=iconv('UTF-8','ASCII//IGNORE//TRANSLIT',$message);
$post_数据=[
'app_id'=>$app_id,
'app_secret'=>$app_secret,
“密码短语”=>$passphrase,
'message'=>rawurlencode($str),
“地址”=>$address
];
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
//执行!
$response=curl\u exec($ch);
$http\u code=curl\u getinfo($ch,CURLINFO\u http\u code);
$queue_id=$records_data['id'];
$invoice_number=$records_data['InvoicecNumber'];
$status_remark=$records_data['Remarks'];
$record_id=$records_data['record_id'];
如果($http|u代码==201 |$$http|u代码==200){
$no_of_trytes=$records_data['no_of_trytes'];
如果($no\u of\u trys==0){
$no_of_尝试=1;
}否则{
$no_of_trytes=$records_data['no_of_trytes'];
}
$status='sent';
$retry=0;
}否则{
$no_of_trys=$records_data['no_of_trys']+1;
如果($no\u of\u trys==3){
$status='失败';
$retry=0;
}否则{
$status='重试';
$retry=1;
}
}
$update\u queued\u data=$this->New\u Sms\u Api\u model->update\u queued\u data($queue\u id,$invoice\u number,$status,$retry,$no\u尝试次数);
if($update\u queued\u data){
如果($status=='sent'){
如果($status\u remark=='LOADED'){
$sent_to=$records_data['sent_to'];
}否则{
$sent_to=NULL;
}
$this->New_Sms_Api_model->save_to_cq_Sms($invoice_number、$status_remark、$record_id、$sent_);
echo$records\u data['record\u id'].----.$status;
}
$limit++;
}
}
}
//关闭连接,释放已使用的资源
卷曲关闭($ch);
}
我们有一条157个字符(最多160个)的消息。我已经和我们正在使用的API支持人员谈过了。首先,他们建议将我的消息格式化为URL编码,我也这样做了。所以从三部分的信息变成了两部分。然后他们说它将作为两部分发送,即使它的格式是这样的,因为我们使用cURL。他们建议我们使用邮递员,但这不是免费的,所以这不是一个选项
有什么想法可以取代我目前的代码吗?谢谢 很抱歉造成混乱。我能够在不更改所有代码的情况下修复该问题。我刚刚删除了字符串消息中的
rawurlencode
,它现在正在发送一个由一部分组成的消息
可能使用$str=iconv('UTF-8','ASCII//IGNORE//TRANSLIT',$message)
已经完成了这个技巧,在添加了额外的字符后使用了rawurlencode
,例如:%20
谢谢大家 我不明白你的两个问题(1和2)。“视图在循环内不工作”?那么cURL如何让它跨越两条短信呢?字符数还是没有?无论如何,这个问题还不清楚