我查看了所有其他终端与PHP的问题,但无法找到解决方案。
我正在处理数据API,这可以检索文件列表:
翻译中缺少什么?
PHP CODE:
$url = 'https://api-url';
$data = array(
'header' => array(
'id' => '1',
),
'others' => array(
"details" => yes,
),
);
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
var_dump($result);
终端代码:(正常工作)
curl -X POST -d '{"header": {"id": 1}, "others": {"details": yes}' --user "u-p" 'https://api-url'
答案 0 :(得分:2)
1)您忘记翻译--user
选项。 PHP libcurl中的对应项是CURLOPT_USERPWD
选项。
2)正如评论中已经写的那样,您必须通过将CURLOPT_SSL_VERIFYPEER
选项设置为false
来禁用SSL对等验证。
3)您不应该同时设置CURLOPT_CUSTOMREQUEST
和CURLOPT_POST
个选项,只能设置其中一个(CURLOPT_POST
)。
所以代码应如下所示:
...
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_USERPWD => 'username/password',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data_string,
CURLOPT_HTTPHEADER => array(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string)
),
));
$result = curl_exec($ch);
var_dump($result);
答案 1 :(得分:1)
尝试为ssl添加
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
此外,您需要检查您的API主机并发送数据,如下所示: -
"{"result":"failed","errorMessage":"expected HTTP authorization, \"client_id\" property, or \"integration\" property","errorCode":"system.error"}"