我试图在PHP中使用ChannelAdvisor REST来加载自上次同步以来更改的股票级别列表。这个过程记录在这里:http://developers.channeladvisor.com/rest/#946
基于这些文档,我在Post (chrome客户端)中运行了一个测试,以确保它按预期工作&它确实是这样的:

由于测试成功,我开始使用PHP脚本,以同样的方式与REST交互,而且它的工作方式不太正确。我得到以下输出:
错误请求- HTTP错误400。请求的格式很差。
到目前为止,这是我的脚本(我正在使用Laravel 4,但它与此无关)。问题在于curlGET方法:
<?php namespace Latheesan\ThirdParty\ChannelAdvisorREST;
class ChannelAdvisorREST {
/**
* ChannelAdvisor base uri
*/
const BASE_URL = 'https://api.channeladvisor.com';
/**
* ChannelAdvisor config data
*/
private $config;
/**
* Class constructor
*/
public function __construct()
{
$this->config = \Config::get('channeladvisor');
}
/**
* Method to load stock updates since last sync.
*
* @param $accountId
* @param $lastSync
* @return array
*/
public function getStockUpdates($accountId, $lastSync)
{
// Anticipate errors
try
{
// Init
$stockUpdates = [];
// Query channel advisor
$stockUpdateResults = self::curlGET($accountId, '/v1/Products?$filter=QuantityUpdateDateUtc gt '. $lastSync);
// TODO: parse $stockUpdateResults into $stockUpdates
// Success
return $this->successResponse($stockUpdateResults);
}
catch (\Exception $ex)
{
// Error response
return $this->errorResponse('Failed to load stock updates - '. $ex->getMessage());
}
}
/**
* Generic method to output error responses.
*
* @param string $message
* @return array
*/
private function errorResponse($message = '')
{
// Error
return [
'IsError' => true,
'ErrorMsg' => $message,
'Data' => ''
];
}
/**
* Generic method to output success responses.
*
* @param $data
* @return array
*/
private function successResponse($data)
{
// Success
return [
'IsError' => false,
'ErrorMsg' => '',
'Data' => $data
];
}
/**
* Method to get access token from rest server.
*
* @param $accountId
* @return string
* @throws \Exception
*/
private function getAccessToken($accountId)
{
// Define cache key
$cache_key = 'CA_REST_ACCESS_TOKEN.'. $accountId;
// Check if there is a cached version of access token
if (\Cache::has($cache_key))
return \Cache::get($cache_key);
// Anticipate errors
try
{
// Call rest api server
$response = 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
]);
// Check if there was an error
if (isset($response->Message))
throw new \Exception($response->Message);
if (isset($response->error))
throw new \Exception($response->error);
// Check if there was an invalid response
if (!isset($response->access_token) || !isset($response->expires_in))
throw new \Exception('Invalid response - '. json_encode($response));
// Cache server response
\Cache::add($cache_key, $response->access_token, floor($response->expires_in / 60));
// Success
return $response->access_token;
}
catch (\Exception $ex)
{
// Rethrow error
throw new \Exception('Failed to load rest api access token - '. $ex->getMessage());
}
}
/**
* Method to generate a HTTP POST request
*
* @param $endpoint
* @param array $fields
* @return string
* @throws \Exception
*/
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) {
$curlError = 'Error #'. curl_errno($ch) .':'. htmlspecialchars(curl_error($ch));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
$curlError .= "\r\nDebug Info: ". htmlspecialchars($verboseLog);
curl_close($ch);
throw new \Exception($curlError);
}
@fclose($verbose);
// Close connection
curl_close($ch);
// Finished
return json_decode($result);
}
/**
* Method to generate HTTP GET request
*
* @param $accountId
* @param $queryString
* @return string
* @throws \Exception
*/
private function curlGET($accountId, $queryString)
{
// Open connection
$ch = curl_init();
// Set the url, query string & access token
curl_setopt($ch, CURLOPT_URL, self::BASE_URL . $queryString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '. self::getAccessToken($accountId)
));
curl_setopt($ch, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $verbose);
// Execute post request
$result = curl_exec($ch);
// TESTING
var_dump($result); exit;
// TESTING
// Debug error
if ($result === FALSE) {
$curlError = 'Error #'. curl_errno($ch) .':'. htmlspecialchars(curl_error($ch));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
$curlError .= "\r\nDebug Info: ". htmlspecialchars($verboseLog);
curl_close($ch);
throw new \Exception($curlError);
}
@fclose($verbose);
// Close connection
curl_close($ch);
// Finished
return json_decode($result);
}
}上面是一个外观类,所以我这样使用它:
echo '<pre>';
print_r(ChannelAdvisorREST::getStockUpdates
(
'xxx-xxx-xxx', // accountId
'2016-01-18T11:50:03.000Z' // lastSync utc date & time
));
echo '</pre>';知道为什么它在Postman客户机中工作,但在我的PHP中失败了吗?我以为我做的是完全一样的步骤。我也尝试过urlencode查询字符串;但这也不起作用。
发布于 2016-01-18 14:30:44
我已经(暂时)像这样解决了它,它对我来说是正确的。如果有更好的方法做这件事,请告诉我。
/**
* Method to generate HTTP GET request
*
* @param $accountId
* @param $queryString
* @return string
* @throws \Exception
*/
private function curlGET($accountId, $queryString)
{
// Open connection
$ch = curl_init();
// Set the url, query string & access token
curl_setopt($ch, CURLOPT_URL, self::BASE_URL . self::formatUri($queryString));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '. self::getAccessToken($accountId)
));
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) {
$curlError = 'Error #'. curl_errno($ch) .':'. htmlspecialchars(curl_error($ch));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
$curlError .= "\r\nDebug Info: ". htmlspecialchars($verboseLog);
curl_close($ch);
throw new \Exception($curlError);
}
@fclose($verbose);
// Close connection
curl_close($ch);
// Finished
return json_decode($result);
}
/**
* Method to format query string
*
* @param $queryString
* @return string
*/
private function formatUri($queryString)
{
return str_replace(
['$', ' '],
['%24', '%20'],
$queryString
);
}https://stackoverflow.com/questions/34855341
复制相似问题