我在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);
答案 0 :(得分:3)
看起来你只是错过了认证片段:
curl_setopt( $ch2, CURLOPT_USERPWD, "yourUsername:yourPassword");
查看manual。此外,你可以这样做,这可以更容易一点:
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'
);