Home > Back-end >  Post product with product image in shopify using php-curl
Post product with product image in shopify using php-curl

Time:01-03

        $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);

I was trying to send my product details via php-curl but when I send it, it shows the data in the site but product's image and price is not showing in it and it's not showing any kind of error.

CodePudding user response:

Use this:


$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;

References: https://shopify.dev/api/admin-rest/2022-10/resources/product#post-products

  • Related