如何在PHP CURL调用中设置APIKEY以了解IBM自然语言

如何在PHP CURL调用中设置APIKEY以了解IBM自然语言,php,curl,ibm-watson,Php,Curl,Ibm Watson,我试图在CURL中设置APIKEY值,以便在PHP应用程序中对IBM自然语言理解API进行身份验证 我使用…CURLOPT_USERPWD,“$usname:$pword”…没有问题。。。选项,但我不确定如何使用新的API apikey:“xxxxx”方法。现在只提供了一个apiKey 我还尝试过将授权apikey放入我的$header_args部分,如下所示: $header_args = array( 'Accept: application/json', 'Content-Ty

我试图在CURL中设置APIKEY值,以便在PHP应用程序中对IBM自然语言理解API进行身份验证

我使用…CURLOPT_USERPWD,“$usname:$pword”…没有问题。。。选项,但我不确定如何使用新的API apikey:“xxxxx”方法。现在只提供了一个apiKey

我还尝试过将授权apikey放入我的$header_args部分,如下所示:

$header_args = array(
   'Accept: application/json',
   'Content-Type: application/x-www-form-urlencoded',
   'Authorization: apikey:xxxxxxx
);
//通过curl设置REST调用的选项

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpointurl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); //set to 'true' so array returns to $result
//curl_setopt($curl, CURLOPT_USERPWD, "$usname:$pword"); <= this works fine
curl_setopt($curl, CURLOPT_USERPWD, 'apikey:' . $apikey);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header_args);
$result = curl_exec($curl);
$curl=curl_init();
curl_setopt($curl,CURLOPT_URL,$endpointurl);
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true)//设置为“true”,使数组返回$result

//curl_setopt($curl,CURLOPT_USERPWD,$usname:$pword”);据我所知,您的代码似乎是正确的。您是否仔细检查了您是否使用了正确的访问令牌?请按您使用的方式使用
“$usname:$pword”
替换
apikey
。如果仍然出现未经授权的错误,请检查正在使用的url端点。端点因创建Watson服务实例所在的区域而异。因此,如果您有
Frankfurt
apikey,它对
London
绑定服务无效。