Shauno_88
Shauno_88

Reputation: 665

PHP REST post with Shopify API

I have a shopify store and would like to add approximately 200 orders using the Shopify API https://help.shopify.com/api/reference/order#create

I've never used PHP before but have put together the below script from previous questions and using the API example data and using a product from my store (variant_id).

Unfortunately this is not placing an order with my store, but does return data for the last order placed with the store, despite not making a GET request. Would you say this is happening because of a syntax error, or am I just structuring the PHP incorrectly? Thanks.

<?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" => "[email protected]"
            ),
            "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" => "[email protected]",
            ));

$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";
}

?>

Upvotes: 1

Views: 7815

Answers (1)

Josh Brown
Josh Brown

Reputation: 4096

Here's an example using PHP and the cURL extension:

<?php
$ch = curl_init("https://key:[email protected]/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);

Upvotes: 3

Related Questions