Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将trustpilot独特的客户链接功能与php和curl集成_Php_Curl_Trustpilot - Fatal编程技术网

将trustpilot独特的客户链接功能与php和curl集成

将trustpilot独特的客户链接功能与php和curl集成,php,curl,trustpilot,Php,Curl,Trustpilot,我的需要是为Trustpilot widget的客户生成唯一链接。为此,我需要一个访问令牌。我尝试了所有可能的方法,但遇到了相同的错误 {“原因”:“未知授予类型”} 下面是我的代码 $data = array( GRANT_TYPE => GRANT_TYPE_VALUE, TRUSTPILOT_USERNAME => TRUSTPILOT_USERNAME_VALUE, TRUSTPILOT_PASSWORD => TRUSTPILOT_PASSWORD_VALUE, );

我的需要是为Trustpilot widget的客户生成唯一链接。为此,我需要一个访问令牌。我尝试了所有可能的方法,但遇到了相同的错误

{“原因”:“未知授予类型”}

下面是我的代码

$data = array(
GRANT_TYPE => GRANT_TYPE_VALUE,
TRUSTPILOT_USERNAME => TRUSTPILOT_USERNAME_VALUE,
TRUSTPILOT_PASSWORD => TRUSTPILOT_PASSWORD_VALUE,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,“https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken”);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Authorization: Basic base64 encoded apikey:secretkey’,
‘Content-Type: application/json’
));
$server_output = curl_exec($ch);
curl_close($ch);
var_dump($server_output);

提前感谢

您是否尝试将
内容类型
设置为
应用程序/x-www-form-urlencoded

我让它工作了,但我必须做一些事情才能让它工作

有效负载数据必须使用
http\u build\u query
进行转换,就像这样

$data = http_build_query(array(
  'grant_type' => 'password',
  'username' => $email,
  'password' => $password,
));
然后curl在验证来自服务器的SSL证书时遇到问题。因此我添加了以下两行:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
更改为的标题值

$authorization = 'Basic '. base64_encode($key . ':' . $secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: ' . $authorization,
  'Content-Type: application/x-www-form-urlencoded'
 ));

最后一段代码:

$data = http_build_query(array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
));
$authorization = 'Basic '. base64_encode($key . ':' . $secret);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: ' . $authorization,
  'Content-Type: application/x-www-form-urlencoded'
 ));

希望这能对您有所帮助。

是的,我尝试了应用程序/x-www-form-urlencoded,但结果是一样的当我添加它时,我开始得到
“原因”:“身份验证失败”
http\u build\u查询应该只调用一次……如果您删除两个调用中的一个,它就可以工作。