[英]PHP REST post with Shopify API
我有一个shopify商店,并希望使用Shopify API添加大约200个订单https://help.shopify.com/api/reference/order#create
我以前从未使用过PHP,但是已经将以前的问题放在一起,使用了API示例数据并使用了我的商店中的产品(variant_id)。
不幸的是,这不是我的商店下订单,但确实返回了商店的最后一个订单的数据,尽管没有发出GET请求。 您是否会因为语法错误而发生这种情况,或者我只是错误地构造了PHP? 谢谢。
<?php $api_key = 'api-key here'; $api_pass = 'api-password here'; $api_url = 'https://' . $api_key . ':' . $api_pass . '@croft-watches.myshopify.com'; $order_url = $api_url . '/admin/orders.json'; $data_array = array("order" => array( "line_items" => array( array( "variant_id" => 10353765828, "quantity" => 1 ) ), "customer" => array( "first_name" => "Paul", "last_name" => "Norman", "email" => "paul.norman@example.com" ), "billing_address" => array( "first_name" => "John", "last_name" => "Smith", "address1" => "123 Fake Street", "phone" => "555-555-5555", "city" => "Fakecity", "province" => "Ontario", "country" => "Canada", "zip" => "K2P 1L4" ), "shipping_address" => array( "first_name" => "Jane", "last_name" => "Smith", "address1" => "123 Fake Street", "phone" => "777-777-7777", "city" => "Fakecity", "province" => "Ontario", "country" => "Canada", "zip" => "K2P 1L4" ), "email" => "jane@example.com", )); $json_data = json_encode($data_array); var_dump($json_data); $options = array( 'https' => array( 'header'=> "Content-Type: application/json\\r\\n" . "Accept: application/json\\r\\n" . "Content-Length: " . strlen($json_data) . "rn", 'method' => 'POST', 'content' => $json_data ) ); $context = stream_context_create($options); $result = file_get_contents($order_url, false, $context); if ($result) { echo $result; } else { echo "post failed"; } ?>
这是使用PHP和cURL扩展的示例:
<?php
$ch = curl_init("https://key:pass@yourstore.myshopify.com/admin/orders.json");
$order = array('order' => array(
'line_items' => array(
array(
'title' => 'Big Brown Bear Boots',
'price' => '74.99',
'grams' => '1300',
'quantity' => 3,
),
array(
'title' => 'Clicky Keyboard',
'price' => '150',
'quantity' => 1,
)
)
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$response = curl_exec($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.