使用php-curl在shopify中发布带有产品图片的产品

vwkv1x7d  于 2023-01-04  发布在  PHP
关注(0)|答案(1)|浏览(147)
$price = $_POST['price'];
        $images = $_POST['images'];
        
  $productData = [
        "product" => [ 
        "variants" => [
            [           
               "price"=>"$price"
            ]
        ],
        "images" => [
            [
               "src"=> "$images"
            ]
        ],
    ]
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://{shop-name}.myshopify.com/admin/api/2022-10/products.json?access_token={access-token}');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type' => 'application/json',
    ]);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($productData));
    $response = curl_exec($ch);
    echo'<pre>';
    print_r($response);
    echo'</pre>';
    curl_close($ch);

我试图通过php-curl发送我的产品详细信息,但当我发送它时,它显示了网站中的数据,但产品的图像和价格没有显示在它,它没有显示任何类型的错误。

qnakjoqk

qnakjoqk1#

请使用此选项:

$post_data=[
 "product"=> [
        "title"=> "Burton Custom Freestyle 151",
        "body_html"=> "<strong>Good snowboard!</strong>",
        "vendor"=> "Burton",
        "product_type"=> "Snowboard",
        "images"=> [
            [
                "src"=> "https://dummyimage.com/600x400/000/fff"
            ]
        ]
    ]
];
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://your-development-store.myshopify.com/admin/api/2022-10/products.json',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => json_encode($post_data),
  CURLOPT_HTTPHEADER => array(
    'X-Shopify-Access-Token: {access_token}',
    'Content-Type: application/json'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

参考文献:https://shopify.dev/api/admin-rest/2022-10/resources/product#post-products

相关问题