2017-04-17 77 views
1

我在嘗試爲產品拉圖像時在我的響應中收到NULL。要求產品清單工作正常。我正在使用Bigcommerce API的第2版。我錯過了什麼? 我的代碼如下:。試圖獲得使用php bigcommerce圖書館的產品圖像列表

$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 

發送請求功能

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); 
    } 
+1

你可以檢查curl拋出的錯誤http://stackoverflow.com/questions/3987006/how-to-catch-curl-errors-in-php – Samir

+1

謝謝使用捲曲我發現我的端點不退出,拿出api /在端點網址,它​​的工作原理。再次感謝 – adjoke

回答