使用Shopify API在PHP中创建产品

时间:2015-02-05 07:35:11

标签: php curl shopify

非常感谢您的解决方案。我正在使用Shopify API和PHP CURL POST方法来创建产品。以下是代码和输出。

$products_array = array(
    "product"=>array(
        'title'=>'',
        "title"=> "Burton Custom Freestlye 151",
        "body_html"=> "<strong>Good snowboard!</strong>",
        "vendor"=> "Burton",
        "product_type"=> "Snowboard",
        "published"=> false ,
        "variants"=>array(
                        array(
                        "sku"=>"t_009",
                        "price"=>20.00,
                        "grams"=>200,
                        "taxable"=>false,
                        )
        )
    )
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password@hostname/admin/products.json";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response); 

我使用$ url部分中提供的凭据。我收到了以下错误

{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}

HTTP/1.1 400 Bad Request
Server: nginx
Date: Thu, 05 Feb 2015 07:28:58 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Sorting-Hat-PodId: 0
X-Sorting-Hat-ShopId: 7628323
X-Sorting-Hat-PodId-Cached: 0
X-Sorting-Hat-ShopId-Cached: 0
Status: 400 Bad Request
X-XSS-Protection: 1; mode=block; report=/xss-report/2dfd1c1e-6c9c-4024-a9bf-f3f6b177eb17?source%5Baction%5D=create&source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin
X-Content-Type-Options: nosniff
X-ShopId: 7628323
X-ShardId: 0
X-Shopify-Shop-Api-Call-Limit: 1/40
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
X-Stats-UserId: 0
X-Stats-ApiClientId: 619259
X-Stats-ApiPermissionId: 9949639
X-Request-Id: 2dfd1c1e-6c9c-4024-a9bf-f3f6b177eb17
X-Kafka-Logged: 1

{"errors":{"product":"Required parameter missing or invalid"}}

1 个答案:

答案 0 :(得分:6)

尝试将Accept: application/json更改为Content-Type: application/json