PHP CURL使用包含美元符号的uri获取请求返回400 Bad Request

时间:2016-01-18 12:56:07

标签: php rest curl

我正在尝试使用PHP中的ChannelAdvisor REST API来加载自上次同步以来更改的库存水平列表。此过程记录在此处:http://developers.channeladvisor.com/rest/#946

根据文档,我在Post Man(chrome rest客户端)中运行了一个测试,以确保它按预期工作&它确实:

enter image description here

由于测试成功,我开始使用PHP脚本以相同的方式与REST API进行交互,但它不能正常工作。我得到以下输出:

  

错误请求 - 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);
    }
}

上面是一个façade类,所以我这样使用它:

echo '<pre>';
print_r(ChannelAdvisorREST::getStockUpdates
(
    'xxx-xxx-xxx',              // accountId
    '2016-01-18T11:50:03.000Z'  // lastSync utc date & time
));
echo '</pre>';

知道为什么它在Postman REST Client中有效,但我的PHP失败了吗?我以为我正在做同样的步骤。我也尝试了urlencode查询字符串;但这也不起作用。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题(现在),这对我来说是正常的。如果有更好/正确的方法,请告诉我。

/**
 * 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
    );
}