简体   繁体   中英

Using cURL to add items to Shopify cart

I am attempting to remotely add an item to a Shopify cart using the Shopify API. I am doing this in PHP with cURL as follows:

$link   = $api_url . '/cart/add.js'; // http://usr:pass@the-shop.myshopify.com/cart/add.js
$vars   = 'id=10672241284';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec ($ch);

curl_close ($ch);

However this is not actually posting anything. I have also tried (with no vars):

$link   = $api_url . '/cart/add/10672241284';

But nothing happens. I have errors turned on but nothing returns. I use echo $link; and then paste that link into the address bar and it works fine, so it isn't the link that's being generated... Am I using cURL or the Shopify API wrong?

UPDATE: As suggested by @Matt:

echo "Curl Error No:" . curl_errno($ch); // returns "0"
echo "Curl HTTP Code:" . curl_getinfo($ch, CURLINFO_HTTP_CODE); // returns "400"

You are barking up the wrong tree. Try using Javascript. The right tool for the job. Executing PHP to add something to cart? Can you explain the need for that? Makes no sense as no browser executes PHP client-side, but all execute JS client-side, which is what you need here. Why are you trying server-side code to add something to a cart?

Hey you need to use shopify admin api (REST APIS) in php not the cart js method as it is only used in javascript front end not server end. Or if you want customize at cart at front end you need to use shopify scripts https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/shopify-scripts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM