【发布时间】:2017-02-21 03:28:00
【问题描述】:
我需要在 PHP 中重新创建它:
curl -X POST --user <username>:<password>
--header "Content-Type: text/plain;charset=utf-8"
--header "Accept: application/json"
--data-binary @<filename>
"https://gateway.watsonplatform.net/personality-insights/api/v3/profile"
我有这个:
$request_headers = array();
$request_headers[] = 'Content-Type: text/plain;charset=utf-8';
$request_headers[] = 'Content-Language: en';
$request_headers[] = 'Accept-Language: en';
$simple_data = 'washingtonpost by the intelligence community';
curl_setopt_array( $ch2, array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $simple_data,
CURLOPT_HEADER => $request_headers,
CURLOPT_USERPWD => 'XXXX:YYYY',
)
);
$response2 = curl_exec( $ch2 );
我的代码没有考虑到 --data-binary 部分,但我不确定如何将其“翻译”成 PHP。另外,我可以使用纯文本(API 接受)的数据二进制而不是 JSON 吗?
【问题讨论】:
-
在您的代码中,
CURLOPT_HEADER应该是CURLOPT_HTTPHEADER以设置请求标头。否则,POSTFIELDS 与自定义 Content-Type 一样正确。 -
@drew010 这正是问题所在!谢谢!如果您提交作为答案,我可以接受。欣赏它。
标签: php curl ibm-watson