[英]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.