php - 如何在php数组json中使用curl发送帖子数据
问题描述
如何在php数组json中使用curl发送帖子数据
像数组一样发布 curl 参数 orderlist 这是我的代码
<?php
class PostOrderPolicy extends CI_Controller{
public function index(){
$data = $this->getData();
foreach ($data as $key) {
$orderListt = array(
'orderDate' => $key['order_date'],
'IDNumber' => $key['IDnumber'],
'itemDescription' => $key['description']
);
$send = $this->sendData($orderListt, $key['resi_number']);
if ($send) {
$res["message"] = "Sukses";
}else{
$res["message"] = "Gagal";
}
}
//echo json_encode($res);
}
private function getData(){
$sql = "SELECT * FROM t_transaction WHERE status='00'";
return $this->db->query($sql)->result_array();
}
private function sendData($data){
$result = array();
$url = "https://contoh.co.id/createPolicy";
$res = array();
$sign = $this->getSign();
$upperSign = strtoupper($sign);
//$random = $this->getRandomStr();
$random = '049KN1PSOL16QHIF';
$batch = 'BATCH000002';
$dataArr = array(
'sign' => $upperSign,
'randomStr' => $random,
'batchNo' => $batch,
'orderList' => [$data]
);
$string = json_encode($dataArr);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$response = json_decode(curl_exec($ch), TRUE);
print_r($response);
print_r($string);
}
}
?>
但我的结果帖子字段 [在此处输入图像描述][1] [1]:https://i.stack.imgur.com/b7LEP.png
我想要这样 [在此处输入图像描述][2] [2]:https://i.stack.imgur.com/xExJ2.png
{"sign":"35EAF78F95A52DDA79FC9911559026F6","randomStr":"049KN1PSOL16QHIF","batchNo":"BATCH000002","orderList":[{"orderDate":"2020-07-01 11:00:00.000","resiNumber":"99875646957","itemDescription":"testing"},{"orderDate":"2020-07-01 ]11:00:00.000","IDNumber":"28929740170","itemDescription":"testing"}]}
解决方案
返回响应而不是输出:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
执行 POST 请求:
$result = curl_exec($ch);
关闭 cURL 资源:
curl_close($ch);
推荐阅读
- asp.net-mvc - 使用 customErrors mode="On" 的 HTTP 错误不一致
- sap - 传递 CLOB TO Sys 过程 SAP HANA
- google-app-engine - 与 MongoDB Atlas 等效的 Google App Engine VPC 对等互连
- rabbitmq - 什么时候从队列中删除消息?
- html - 将 LocalStorage 数据动态解析成一张表
- python - pywinAuto - 使用 Windows MFC 控件
- actions-on-google - Google 助理可以访问网络帖子上的音频吗
- php - PHP SQL Server连接工作或不工作
- reactjs - 改进对 React 的错误处理 | 还原 | MERN 应用程序
- html - XSLT:如何避免在 HTML 的属性中添加自闭合标签