PHP REST post 与 Shopify API
PHP REST post with Shopify API
我有一个 shopify 商店,我想使用 Shopify 添加大约 200 个订单 API https://help.shopify.com/api/reference/order#create
我以前从未使用过 PHP,但将之前问题中的以下脚本放在一起,并使用 API 示例数据和我商店的产品 (variant_id) .
不幸的是,这不是在我的商店下订单,而是 return 上次在商店下的订单的数据,尽管没有发出 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);
我有一个 shopify 商店,我想使用 Shopify 添加大约 200 个订单 API https://help.shopify.com/api/reference/order#create
我以前从未使用过 PHP,但将之前问题中的以下脚本放在一起,并使用 API 示例数据和我商店的产品 (variant_id) .
不幸的是,这不是在我的商店下订单,而是 return 上次在商店下的订单的数据,尽管没有发出 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);