2016-08-08 31 views
0

ich einen Shopify Speicher und mag etwa 200 Aufträge hinzufügen mit der Shopify API https://help.shopify.com/api/reference/order#createPHP REST Beitrag mit Shopify API

Ich habe noch nie PHP verwendet, sondern habe zusammen die unten stehende Skript aus vorherigen Fragen und mit die API-Beispieldaten und die Verwendung eines Produkts aus meinem Geschäft (variant_id).

Leider gibt dies keine Bestellung mit meinem Geschäft, aber gibt Daten für die letzte Bestellung mit dem Geschäft, obwohl keine GET-Anfrage. Würdest du sagen, dass das wegen eines Syntaxfehlers passiert, oder baue ich PHP nur falsch auf? Vielen Dank.

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

 
?>

Antwort

1

Hier ist ein Beispiel PHP und cURL Erweiterung mit:

<?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); 
+0

Dank joshubrown, versuche ich nicht cURL zu verwenden, vor allem, da ich es nicht funktioniert bekommen könnte auf meinem Computer und ich sah andere Antworten hier, die es ohne cURL zu verwalten scheinen. Wenn ich heute Abend Zeit habe, werde ich versuchen, es zu versuchen. –

+0

Ich habe es versucht und es an PHPFiddle arbeiten lassen. Danke für Ihre Hilfe! –

+0

es gibt Fehler Kann nicht POST /admin/orders.json –