繁体   English   中英

将cURL“翻译”成PHP

[英]“Translate” cURL to PHP

我在PHP中努力使用cURL。 我不确定要“翻译”此内容需要做什么:

curl -X POST -u "{username}:{password}" --header "Content-Type: application/json" --data-binary @profile.json "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2016-10-20&consumption_preferences=true&raw_scores=true"

到PHP中执行。

到目前为止,这就是我所拥有的,但是我感觉自己还没有接近:

$url2 = 'https://watson-api-explorer.mybluemix.net/personality-insights/api/v3/profile?raw_scores=false&csv_headers=false&consumption_preferences=true&version=2017-02-01';
$request_headers = array();
$request_headers[] = 'Accept: application/json';
$request_headers[] = 'Content-Type: text/plain';
$request_headers[] = 'Content-Language: en';
$request_headers[] = 'Accept-Language: en';

$ch2 = curl_init( $url2 );
curl_setopt( $ch2, CURLOPT_POST, 1);
curl_setopt( $ch2, CURLOPT_POSTFIELDS, $myvars2);
curl_setopt( $ch2, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch2, CURLOPT_HEADER, $request_headers);
curl_setopt( $ch2, CURLOPT_RETURNTRANSFER, 1);
$response2 = curl_exec( $ch2 );
var_dump($response2);

看来您只是缺少身份验证部分:

curl_setopt( $ch2, CURLOPT_USERPWD, "yourUsername:yourPassword");

查阅手册 另外,您可以通过这种方式进行操作,这可能会容易一些:

curl_setopt_array( $ch2, array(
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $myvars2,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_HEADER => $request_headers,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_USERPWD => 'yourUsername:yourPassword'
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM