[英]Working with ChannelAdvisor REST API in PHP - requests not working in CURL
我想使用SOAP Credentials Flow与ChannelAdvisor REST API集成。
根据他们的文档,我在PostMan
(Chrome浏览器中的其余客户端)中进行了如下设置:
当我休息时 其余api服务器返回预期的响应:
因此,我尝试使用以下类在PHP中复制该代码:
<?php
class ChannelAdvisorREST {
/**
* ChannelAdvisor constants & properties
*/
const BASE_URL = 'https://api.channeladvisor.com/v1';
private $config;
/**
* Class constructor
*/
public function __construct()
{
$this->config = \Config::get('channeladvisor');
}
// TEST
public function test($accountId)
{
// var_dump($this->config);
var_dump(self::getAccessToken($accountId));
}
// TEST
/**
* Method to get access token from rest server.
*
* @param $accountId
* @return string
*/
private function getAccessToken($accountId)
{
return self::curlPOST('/oauth2/token', [
'client_id' => $this->config['api_app_id'],
'grant_type' => 'soap',
'scope' => 'inventory',
'developer_key' => $this->config['api_developer_key'],
'password' => $this->config['api_password'],
'account_id' => $accountId
]);
}
/**
* Method to generate a HTTP POST request
*
* @param $endpoint
* @param $fields
* @return string
*/
private function curlPOST($endpoint, $fields = array())
{
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_USERPWD, $this->config['api_app_id'] .':'. $this->config['api_shared_secret']);
curl_setopt($ch, CURLOPT_URL, self::BASE_URL . $endpoint);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields, '', '&'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
// Execute post request
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// Finished
return $result;
}
}
当我在此类上执行test($ accId)方法时,得到以下响应:
知道为什么它与PostMan
测试不太一样吗?
PS我已经验证了所有配置/参数等...是正确的,并且与我的PostMan测试相同。 此类是我原始代码(在Laravel 4.2中创建,但此问题与Laravel不相关)的摘要版本。
两件事情:
确保发送的标题与浏览器发送的标题相同。 例如,我在您的代码中看不到Authorization-header,对于在服务器端对请求进行授权来说,这一标题可能至关重要。 同样,对于范围,您使用“库存”而不是“订单库存”。 在这个练习中要非常严格。
测试不在数组中的后数据,但应根据自己的情况写下查询字符串,这样您就可以知道CURL尝试将数组转换为查询字符串没有什么问题(请注意,两者可以用于CURL,数组和查询字符串)。
因此最容易测试:
client_id=1234&grant_type=soap&scope=order%20inventory...etc add other variables...
我发现了问题。 该问题是由于我的php未配置curl.cainfo
。
我是通过将以下调试代码添加到我的curlPOST
方法中来找到的,如下所示:
private function curlPOST($endpoint, $fields = array())
{
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_USERPWD, $this->config['api_app_id'] .':'. $this->config['api_shared_secret']);
curl_setopt($ch, CURLOPT_URL, self::BASE_URL . $endpoint);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields, '', '&'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// Execute post request
$result = curl_exec($ch);
// Debug error
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($ch), htmlspecialchars(curl_error($ch)));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
}
@fclose($verbose);
// Close connection
curl_close($ch);
// Finished
return $result;
}
这将输出以下错误消息:
cUrl error (#60): SSL certificate problem: unable to get local issuer certificate
Verbose information:
* Hostname was found in DNS cache
* Hostname in DNS cache was stale, zapped
* Trying 216.27.89.14...
* Connected to api.channeladvisor.com (216.27.89.14) port 443 (#7)
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 7
boolean false
这帮助我用php跟踪了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.