2012-09-06 33 views
0

我正在嘗試使用Sandeep Shetty的shopify.php創建自定義產品集合。香港專業教育學院已經能夠使其他功能API calls-顯示產品和推車等,但林無法創建自定義集合與下面的代碼Shopify:創建新集合的困境

// For private apps: 
$shopify = shopify_api_client(SHOPIFY_DOMAIN, NULL, SHOPIFY_API_KEY, SHOPIFY_API_PASS, true); 

$charge = array 
    (
     "custom_collection"=>array 
     (
      "title"=>"IPods", 
      "collects"=>array(
       "product_id"=>99395358, 
      ) 
     ) 
    ); 
//  pr(json_encode($charge), 1); 
$charge = '{"custom_collection": { "title": "IPods", "collects": [ {"product_id":99395358}, {"product_id":0000000} ] } }'; 


try 
{ 
    // All requests accept an optional fourth parameter, that is populated with the response headers. 
    $my_collect = $shopify('POST', '/admin/custom_collections.json', $charge, $response_headers); 

    // API call limit helpers 
    echo shopify_calls_made($response_headers); // 2 
    echo shopify_calls_left($response_headers); // 298 
    echo shopify_call_limit($response_headers); // 300 

    pr($my_collect); 

} 
catch (ShopifyApiException $e) 
{ 
    pr($e); 
} 
catch (ShopifyCurlException $e) 
{    
    echo "doh"; 
    pr($e); 
} 

在上面要求我得到如下回應,我不知道什麼是錯的在請求,因爲它是

ShopifyApiException Object 
(
[info:protected] => Array 
    (
     [method] => POST 
     [path] => /admin/custom_collections.json 
     [params] => {"custom_collection": { "title": "IPods", "collects": [ {"product_id":99395358}, {"product_id":0000000} ] } } 
     [response_headers] => Array 
      (
       [http_status_message] => Bad Request 
       [http_status_code] => 400 
       [server] => nginx 
       [date] => Thu, 06 Sep 2012 05:55:36 GMT 
       [content-type] => application/json 
       [transfer-encoding] => chunked 
       [connection] => keep-alive 
       [status] => 400 Bad Request 
       [x-runtime] => 0.000986 
      ) 

     [response] => Array 
      (
       [error] => lexical error: invalid char in json text. 
           "{"custom_collection": { "title": 
       (right here) ------^ 

      ) 

    ) 

[message:protected] => Bad Request 
[string:Exception:private] => 
[code:protected] => 400 
[file:protected] => D:\www\jim\shopify\init.php 
[line:protected] => 244 
[trace:Exception:private] => Array 
    (
     [0] => Array 
      (
       [file] => D:\www\jim\shopify\index.php 
       [line] => 31 
       [function] => {closure} 
       [args] => Array 
        (
         [0] => POST 
         [1] => /admin/custom_collections.json 
         [2] => {"custom_collection": { "title": "IPods", "collects": [ {"product_id":99395358}, {"product_id":0000000} ] } } 
         [3] => Array 
          (
           [http_status_message] => Bad Request 
           [http_status_code] => 400 
           [server] => nginx 
           [date] => Thu, 06 Sep 2012 05:55:36 GMT 
           [content-type] => application/json 
           [transfer-encoding] => chunked 
           [connection] => keep-alive 
           [status] => 400 Bad Request 
           [x-runtime] => 0.000986 
          ) 

        ) 

      ) 

    ) 

[previous:Exception:private] => 
) 

回答

0

我得到它的工作

if(count($categories_results) > 0){ 

    foreach($categories_results as $cat){ 

      try 
      { 

       $charge = array 
       (
        "custom_collection"=>array 
        (
         "title"=>$cat['name'], 
        ) 
       ); 

       try 
       { 

        $recurring_application_charge = $shopify('POST', '/admin/custom_collections.json', $charge, $response_headers); 



        $shopify_cat_id = $recurring_application_charge['id']; 
        $shopify_cat_data = serialize($recurring_application_charge); 
        $db->update('categories', array('shopify_id' => $shopify_cat_id, 'shopify_res' => $shopify_cat_data), "id=%d", $cat['id']); 

       } 
       catch (ShopifyApiException $e) 
       { 
        pr($e); 

       } 

      } 
      catch (ShopifyApiException $e) 
      { 
       pr($e); 
      } 
      catch (ShopifyCurlException $e) 
      { 
       pr($e); 
      } 


    } //end of foreach 

} //end of IF 
視爲錯誤的請求