0

我正在尝试使用 Sandeep Shetty 的 shopify.php 创建自定义产品集合。我已经能够进行其他功能性 API 调用 - 显示产品和购物车等。但我无法使用以下代码创建自定义集合

// 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] => 
)
4

1 回答 1

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
于 2012-09-20T07:14:40.630 回答