我在bigcommerce工作,我需要做一些Bigcommerce以curl格式提供代码的东西...我不知道卷曲......你能告诉我如何在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
任何帮助都将受到高度赞赏......
答案 0 :(得分:2)
$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);
答案 1 :(得分:1)
您可以在PHP中尝试以下操作,但请确保已安装该卷曲扩展程序 http://www.php.net/manual/en/curl.installation.php
代码为
$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);
确保该行
curl_setopt($ ch,CURLOPT_HTTPHEADER,array('Content-Type:application / json','Content-Length:'。strlen($ postfields)));
strlen($ postfields)永远不会为0,换句话说你应该有Post数据