Question

I am working in bigcommerce and I need something done for which Bigcommerce has provided the code in curl format...I do not know curl well...Can you tell me how can I execute the following code in php??

curl --request POST \
-u "admin:key" \
-H "Content-Type: application/json" \
-d '{"order_address_id":15,"tracking_number":"123-123-123","items":[{"order_product_id":15,"quantity":1}]}' \
https://store/api/v2/orders/114/shipments.json

Any help would be highly appreciated...

Was it helpful?

Solution

$ch = curl_init('https://store/api/v2/orders/114/shipments.json');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:key');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, 
    json_encode(array('order_address_id' => 15,
                      'tracking_number' => '123-123-123',
                      'items' => array(
                            array('order_product_id' => 15, 'quantity' => 1)
                        )
                     )));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

OTHER TIPS

You can try as below in PHP, but make sure that that curl extension is installed http://www.php.net/manual/en/curl.installation.php

The code goes as

$postfields = '{"order_address_id":15,"tracking_number":"123-123-123","items":[{"order_product_id":15,"quantity":1}]}' ;

$ch = curl_init(); 
curl_setopt( $ch, CURLOPT_URL, 'https://store/api/v2/orders/114/shipments.json');
curl_setopt( $ch, CURLOPT_TIMEOUT, 100 );
curl_setopt($ch, CURLOPT_USERPWD, 'admin:key');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($postfields))); 
$response = curl_exec($ch);
curl_close ($ch);

Make sure that the line

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($postfields)));

the strlen($postfields) is never 0 or in other words you should have the Post data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top