php - PHP cURL 正文不适用于多个数组
问题描述
我需要通过cURL
in发送数据PHP
。有我的POST
请求,它适用于这个例子(我可以发布 1 条记录)。
$postData = array(
"username" => "email@domain.com",
"name" => "Name",
"surname" => "Surname",
'role' => 'user',
"disabled" => "false"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt_array($ch,
array(
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
// ,CURLOPT_POSTFIELDS => json_encode($postData)
));
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE); // this results 0 every time
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
return stripslashes($response);
curl_close($ch);
但我想将数据库中$postData
array
的记录添加到但它停止了。
这是我的数据库数组示例:
$postData = array();
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
array_push($postData, $row);
}
json
数组看起来ok
,但我找不到错误。或者我不能发布多个数据,我需要循环添加所有内容?
解决方案
我在代码中发现了一些缺陷。您说您正在发布代码,但您已设置
CURLOPT_POST => false
另外,你重复了
CURLOPT_RETURNTRANSFER => TRUE and CURLOPT_RETURNTRANSFER => 1
此外,您的 API 可能设计为接受一条记录,但您尝试发送多条记录。如果它被设计为一次接受 1 条记录,你应该让 curl 调用一个函数并调用它来代替 array_push。例如:。卷曲函数($行);
function curlfunction($postData){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->getCurlUrl());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST , 1);
curl_setopt_array($ch, CURLOPT_HTTPHEADER, array(
'authorization: apikey '.$apikey,
'Content-Type: application/rest+json',
'pincode: '.$pincode
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS , json_encode($postData));
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
curl_close($ch);
return stripslashes($response);
}
和
foreach ($records = $this->getRecords as $value) {
$row['username'] = $value['email'];
$row['name'] = $value['name'];
$row['surname'] = $value['surname'];
$row['role'] = 'User';
$row['disabled'] = $value['status'];
curlfunction($row);
}
推荐阅读
- jupyter-notebook - jupyter notebook (macOS) 中的 %conda list biopython 错误
- java - 无法加载类“com.intellij.util.Consumer”
- javascript - npm 不支持 Node.js vX.XX(当前)
- php - Paypal 捐赠返回 url 给出了一个空数组
- android-gradle-plugin - Android lint AnnotationProcessorOnCompilePath 与 ButterKnife
- c# - 以编程方式使 UIView 高度包装其内容?
- swift - 对齐文本 Swiftui
- r - 如何从 r 中的箱线图中获取值(例如中位数)?
- sql - Proc oracle 问题 - 调用已执行但没有插入 (ORACLE/PLSQL)
- javascript - 从firebase加载数组后的setState