简体   繁体   English

PHP CURL获取带有美元符号的uri的请求,返回400错误请求

[英]PHP CURL Get request with uri containing dollar sign returns 400 Bad Request

I am trying to use the ChannelAdvisor REST API in PHP to load a list of stock levels that changed since the last sync. 我试图在PHP中使用ChannelAdvisor REST API加载自上次同步以来已更改的库存水平列表。 This process is documented here: http://developers.channeladvisor.com/rest/#946 此过程记录在这里: http : //developers.channeladvisor.com/rest/#946

Based on the documentation, I have ran a test in Post Man (chrome rest client) to make sure It's working as intended & it does: 根据文档,我已经在Post Man(Chrome浏览器余下的客户端)中进行了测试,以确保其按预期工作并且可以:

在此处输入图片说明

Since the test was successfully, I started to work on the PHP script to interact with the REST API in the same manner and it's not quite working correctly. 由于测试成功,因此我开始以相同的方式使用PHP脚本与REST API进行交互,但它无法正常工作。 I am getting the following output: 我得到以下输出:

Bad Request - HTTP Error 400. The request is badly formed. 错误的请求-HTTP错误400。请求的格式错误。

This is my script so far (I am working with Laravel 4, but it's unrelated to that). 到目前为止,这是我的脚本(我正在使用Laravel 4,但与此无关)。 The issue is with the curlGET method: 问题在于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);
    }
}

The above is a façade class, so I use it like this: 上面是一个外观类,所以我这样使用它:

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

Any idea why it works in Postman REST Client, but fails in my PHP? 知道为什么它可以在Postman REST Client中工作,但是在我的PHP中失败吗? I thought I am doing the exact same steps. 我以为我正在做完全相同的步骤。 I tried urlencode the query string also; 我也尝试了urlencode查询字符串; but that did not work either. 但这也不起作用。

I have solved it (for now) like this and it's working correctly for me. 我已经这样解决了(目前),它对我来说正常工作。 Let me know if there's a better/proper way to do this. 让我知道是否有更好/合适的方法可以做到这一点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM