2015-04-29 107 views
1

Im使用插件CakePHP使呼叫獲得某些訂單。我可以打電話給某些領域的所有訂單,但我想知道如何才能打電話來獲得具有特定名稱或訂單號的訂單?以下是致電Shopify的來源。它已經通過身份驗證和一切:Shopify API通過名稱或order_number獲取訂單

public function call($method, $path, $params=array()) 
    { 
     if (!$this->isAuthorized()) 
      return; 
     $password = $this->is_private_app ? $this->secret : md5($this->secret.$this->ShopifyAuth->token); 
     $baseurl = "https://{$this->api_key}:[email protected]{$this->ShopifyAuth->shop_domain}/"; 

     $url = $baseurl.ltrim($path, '/'); 
     $query = in_array($method, array('GET','DELETE')) ? $params : array(); 
     $payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array(); 
    $request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array(); 
    $request_headers[] = 'X-Shopify-Access-Token: ' . $this->ShopifyAuth->token; 
     list($response_body, $response_headers) = $this->Curl->HttpRequest($method, $url, $query, $payload, $request_headers); 
    $this->last_response_headers = $response_headers; 
    $response = json_decode($response_body, true); 

     if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400)) 
      throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response); 

     return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response; 
    } 

    private function shopApiCallLimitParam($index) 
    { 
     if ($this->last_response_headers == null) 
     { 
      return 0; 
     } 
     $params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']); 
     return (int) $params[$index]; 
    } 

...這使得GET調用的代碼:

// I only want the id and title of the collections 
      $fields = "fields=name,id,status,financial_status,fulfillment_status,billing_address,customer"; 
      // get list of collections 
      $custom_collections = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields); 
      $this->set('collections', $custom_collections); 

我覺得我失去了在那裏我可以把條件呼叫的地方得到一定的訂單。我已經閱讀了API文檔,但似乎無法得到答案。

我試圖把?name=%231001的URL .json後,試圖獲得訂單#1001,但它帶回一個空數組。

然後我嘗試?order_number = 1001但它帶給我每一個訂單以及1001 D:這真是令人困惑,任何人都可以幫助我嗎?

在此先感謝。

回答

2

那麼我發現你實際上可以使用名稱或order_number獲得訂單。它的另一個屬性由於某種原因未在文檔中列出。但是在URL中,如果您使用其他語言,則您必須在GET中添加的所有內容是admin/order.json?name =%2310001 & status =任何這樣做都可以獲得訂單10001,因此只需在%23之後添加order_number 。我在Shopify大學的一個論壇上看到這個,我只是在我的代碼上實現了這個錯誤。如果您使用像我一樣的CakePhp shopify插件,我所做的只是在$字段添加?name =%23「。number」。 &狀態=任何「;

生病離開這裏代碼:

$this->layout = 'main'; 

$order_number = "18253"; 

$fields = "name=%23". $order_number ."&status=any"; 

$order = $this->ShopifyAPI->call('GET', "/admin/orders.json", $fields); 
    if (!empty($order)) { 
    $this->set('order', $order); 

    } else { 
     $this->Session->setFlash('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button> No existe el numero de orden ingresado.','default',array('class' => 'alert alert-danger alert-dismissible', 'type' => 'alert')); 
    } 

希望這可以幫助別人:P

+0

如果你必須手動逃脫#在您的查詢字符串,這意味着當時的api調用是通過原始輸入。真正的修復應該在你的HttpRequest類中,它應該是url編碼查詢參數傳入它。http://php.net/manual/en/function.http-build-query.php – txyoji

+0

實際上,編碼#的編號會使我們的商店使用前綴作爲訂單名稱的前綴,其他編號可能會在訂單編號上有其他前綴。 http的工作會更多,因爲我不是每次都會在ShopfyAPI的域上發送名稱來進行調用。 –

+0

我明白name =#000000是格式。我說的是,如果你必須手動轉義一個值(使用%23而不是#),這意味着你的查詢字符串中有一個保留字符(空格,#?&)的任何值都不會被編碼正確。在 - > call()方法中解決這個編碼問題意味着您不必擔心「$ fields」數據中的特殊字符。 http_build_query()可以爲你處理這個細節。 – txyoji