phpbigcommerce

trying to get a list of product images using php bigcommerce library


I keep getting NULL in my response while trying to pull images for products. Making a request for list of products works fine. I'm using the version 2 of the Bigcommerce API. What am I missing? My code is below:

    $product_id = 82;
    $organizationAccount = array(
        "username" => "stores/xxxx",
        "user_token" => "xxxxx"
    );    
    $resource =  'http://api.bigcommerce.com/stores/xxxx/api/v2/products/'.$product_id.'/images.json';

    $response = sendRequest($resource, 'GET', 'user_token'); //This gives null

Send Request function

function sendRequest($url, $method, $userToken, $postFields = null, $queryData = null){

        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

        if( $method == "PUT" ){
            curl_setopt($curlHandle, CURLOPT_HEADER, true);
        }

        curl_setopt($curlHandle, CURLOPT_CUSTOMREQUEST, $method);
        $httpHeader = array('Content-Type: application/json');

        $url = (strpos($url, "http") !== false ) ? str_replace("http", "https", $url) : "https://" . $url;

        if ($method == "POST" || $method == "PUT" || $method == "DELETE") {
            curl_setopt($curlHandle, CURLOPT_URL, $url);
            curl_setopt($curlHandle, CURLOPT_POST, true);
            curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $postFields);
        } elseif ($method == "GET") {
            $url = (is_null($queryData)) ? $url : $url . "?" . http_build_query($queryData);
            curl_setopt($curlHandle, CURLOPT_URL, $url);
        }

        if (!is_null($userToken)) {
            $httpHeader[] = 'X-Auth-Client: ' . BC_CLIENT_ID; //Client id
            $httpHeader[] = 'X-Auth-Token: ' . $userToken;
            //curl_setopt($curlHandle);
            curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $httpHeader);
        }

        $response = curl_exec($curlHandle);

        return json_decode($response, true);
    }

Solution

  • The mistake was from the endpoint url, it is supposed to be

    $resource = 'http://api.bigcommerce.com/stores/xxxx/v2/products/'.$product_id.'/images.json';