我需要用 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 吗?
最佳答案
您所拥有的已经是一个 --data-binary
等价物。查看CURLOPT_POSTFIELDS
API docs :
You must make sure that the data is formatted the way you want the server to receive it. libcurl will not convert or encode it for you in any way.
将其与 docs for the command-line --data-binary
option 进行比较:
This posts data exactly as specified with no extra processing whatsoever.
至于你问题的第二部分:
can I use data binary with plain text (the API accepts it) instead of JSON
是的,命令行中的 --data-binary
和 API 中的 CURLOPT_POSTFIELDS
。
关于php - 将 "data-binary"从 cURL 翻译成 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358104/