php - 将 "data-binary"从 cURL 翻译成 PHP

标签 php curl ibm-watson

我需要用 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/

相关文章:

ibm-cloud - 如何在 Bluemix Watson Visual Recognition 中处理服务绑定(bind)上的 API key 重新生成?

php动态类构建

node.js - 失眠与NodeJS : "Error: Peer certificate cannot be authenticated with given CA certificates"

php - 使用 cURL php 抓取图像

curl - Sonatype Nexus 使用 REST API 添加代理存储库

ibm-watson - 使用 Postman 测试语音转文本 watson API,但出现错误 401

php - 为什么mcrypt中会出现这些奇怪的字符?

javascript - 更改用于加密的主密码

php - 获取在 PHP 中执行当前函数的代码行和文件?

ibm-cloud - Watson Personality Insights 是否使用转发?