Created
February 15, 2012 15:34
-
-
Save ChrisMoney/c3b32d2d50ae6505ac4a to your computer and use it in GitHub Desktop.
API - Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
API Code: | |
<?php | |
//Modify these | |
$API_KEY = 'your-token-here'; | |
$SECRET = 'your-secret-here'; | |
$TOKEN = 'your-secret-here'; | |
$STORE_URL = 'yourestore.myshopify.com'; | |
$PRODUCT_ID = 'product-id-here'; | |
$url = 'https://' . $API_KEY . ':' . md5($SECRET . $TOKEN) . '@' . $STORE_URL . '/admin/products/' . $PRODUCT_ID . '.xml'; | |
$session = curl_init(); | |
curl_setopt($session, CURLOPT_URL, $url); | |
curl_setopt($session, CURLOPT_HTTPGET, 1); | |
curl_setopt($session, CURLOPT_HEADER, false); | |
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml')); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
if(ereg("^(https)",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false); | |
$response = curl_exec($session); | |
curl_close($session); | |
$product_xml = new SimpleXMLElement($response); | |
echo $product_xml->title; | |
echo $product_xml->variants->variant->{'inventory-quantity'}; | |
?> | |
API Example 2: | |
<?php | |
//Modify these | |
$API_KEY = 'xxx'; | |
$SECRET = 'yyy'; | |
$TOKEN = 'zzz'; | |
$STORE_URL = 'store.myshopify.com'; | |
$url = 'https://' . $API_KEY . ':' . md5($SECRET . $TOKEN) . '@' . $STORE_URL . '/admin/products.xml'; | |
$product = new SimpleXMLElement('<product></product>'); | |
$product->addChild('body','this is the description'); | |
$product->addChild('title','this is the title'); | |
$product->addChild('product-type','XHTML & CSS'); | |
$variants = $product->addChild('variants'); | |
$variants->addAttribute('type','array'); | |
$variant = $variants->addChild('variant'); | |
$variant->addChild('price','3.00'); | |
$variant->addChild('option1','Single-Use'); | |
$variant = $variants->addChild('variant'); | |
$variant->addChild('price','333.00'); | |
$variant->addChild('option1','Buyout'); | |
$product->addChild('vendor','JLH'); | |
echo $product; | |
$session = curl_init(); | |
curl_setopt($session, CURLOPT_URL, $url); | |
curl_setopt($session, CURLOPT_POST, 1); | |
curl_setopt($session, CURLOPT_POSTFIELDS, $product); | |
curl_setopt($session, CURLOPT_HEADER, false); | |
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml')); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
if(ereg("^(https)",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false); | |
$result = curl_exec($session); | |
if ( curl_errno($session) ) { | |
$result = 'cURL ERROR -> ' . curl_errno($session) . ': ' . curl_error($session); | |
} else { | |
$returnCode = (int)curl_getinfo($session, CURLINFO_HTTP_CODE); | |
switch($returnCode){ | |
case 200: | |
break; | |
default: | |
$result = 'HTTP ERROR -> ' . $returnCode; | |
break; | |
} | |
} | |
curl_close($session); | |
echo $result; | |
?> | |
API Example 3: | |
<?php | |
//Modify these | |
$API_KEY = 'xxx'; | |
$SECRET = 'yyy'; | |
$TOKEN = 'zzz'; | |
$STORE_URL = 'sss.myshopify.com'; | |
$url = 'https://' . $API_KEY . ':' . md5($SECRET . $TOKEN) . '@' . $STORE_URL . '/admin/products.xml'; | |
$xmlsrc = <<<XML | |
<?xml version='1.0' encoding='UTF-8'?> | |
<product> | |
<title>This is the title.</title> | |
<body>This is the description.</body> | |
<product-type>Photoshop</product-type> | |
<variants type="array"> | |
<variant> | |
<price>3.00</price> | |
<option1>Single-Use</option1> | |
</variant> | |
<variant> | |
<price>5.00</price> | |
<option1>Buyout</option1> | |
</variant> | |
</variants> | |
<vendor>JLH</vendor> | |
</product> | |
XML; | |
$session = curl_init(); | |
curl_setopt($session, CURLOPT_URL, $url); | |
curl_setopt($session, CURLOPT_POST, 1); | |
curl_setopt($session, CURLOPT_POSTFIELDS, $xmlsrc); | |
curl_setopt($session, CURLOPT_HEADER, false); | |
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml')); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
if(ereg("^(https)",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false); | |
$result = curl_exec($session); | |
curl_close($session); | |
echo $result; // this shows exactly what you'd expect to see, as given in API documentation | |
?> | |
API Code Eample 4: | |
<?php | |
include_once ('ActiveResource.php'); | |
define ('SHOPIFY_URL', 'https://APIKEY:' . md5 ('SECRET' . 'TOKEN') . '@SHOPNAME.myshopify.com/admin/'); | |
class Product extends ActiveResource { | |
var $site = SHOPIFY_URL; | |
var $request_format = 'xml'; | |
} | |
$product = new Product (array ( | |
'title' => 'test 2', | |
'body' => 'description', | |
'product-type' => 'Photoshop', | |
'variants' => array ( | |
'variant' => array ( | |
array ('price' => '3.00', 'option1' => 'Single-Use'), | |
array ('price' => '5.00', 'option1' => 'Buyout'), | |
), | |
), | |
'vendor' => 'test', | |
)); | |
$product->save (); | |
header ('Content-Type: text/plain'); | |
echo htmlentities ($product->response_body); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment