Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- define( 'SHOPIFY_DOMAIN', 'domain.myshopify.com');
- define( 'SHOPIFY_API_KEY', 'API KEY');
- define( 'SHOPIFY_API_PASS', 'PASS HERE');
- define( 'SHOPIFY_SECRET', 'SECRET HERE');
- require 'shopify/shopify.php';
- // For regular apps:
- $shopify = shopify_api_client(SHOPIFY_DOMAIN, NULL, SHOPIFY_API_KEY, SHOPIFY_API_PASS, true);
- // For private apps:
- // $shopify = shopify_api_client($shops_myshopify_domain, NULL, $api_key, $password, true);
- try
- {
- // Get all products
- // $products = $shopify('GET', '/admin/products.json', array('published_status'=>'published'));
- // Create a new recurring charge
- $charge = array
- (
- "custom_collection"=>array
- (
- "title"=>"IPods",
- "collects"=>array(
- "product_id"=>99395362
- )
- )
- );
- try
- {
- // All requests accept an optional fourth parameter, that is populated with the response headers.
- $recurring_application_charge = $shopify('POST', '/admin/custom_collections.json', $charge, $response_headers);
- // pr($response_headers);
- // pr($recurring_application_charge);
- // API call limit helpers
- echo shopify_calls_made($response_headers); // 2
- echo shopify_calls_left($response_headers); // 298
- echo shopify_call_limit($response_headers); // 300
- }
- catch (ShopifyApiException $e)
- {
- pr($e);
- // If you're here, either HTTP status code was >= 400 or response contained the key 'errors'
- }
- }
- catch (ShopifyApiException $e)
- {
- // $err = $e->getInfo();
- pr($e);
- /* $e->getInfo() will return an array with keys:
- * method
- * path
- * params (third parameter passed to $shopify)
- * response_headers
- * response
- * shops_myshopify_domain
- * shops_token
- */
- }
- catch (ShopifyCurlException $e)
- {
- pr($e);
- // $e->getMessage() returns value of curl_errno() and $e->getCode() returns value of curl_ error()
- }
- ShopifyApiException Object
- (
- [info:protected] => Array
- (
- [method] => POST
- [path] => /admin/custom_collections.json
- [params] => Array
- (
- [custom_collection] => Array
- (
- [title] => IPods
- [collects] => Array
- (
- [product_id] => 99395362
- )
- )
- )
- [response_headers] => Array
- (
- [http_status_message] => Internal Server Error
- [http_status_code] => 500
- [server] => nginx
- [date] => Sat, 15 Sep 2012 18:56:36 GMT
- [content-type] => application/json; charset=utf-8
- [transfer-encoding] => chunked
- [connection] => keep-alive
- [status] => 500 Internal Server Error
- [x-shopify-shop-api-call-limit] => 1/500
- [http_x_shopify_shop_api_call_limit] => 1/500
- [cache-control] => no-cache
- [x-request-id] => f6fdb72b6cabf22a73207d82b8ea16d9
- [x-ua-compatible] => IE=Edge,chrome=1
- [set-cookie] => _secure_session_id=cf2648422369ee231220b6f1f1efdb3b; path=/; secure; HttpOnly
- [x-runtime] => 0.047004
- )
- [response] => Array
- (
- [errors] => Error
- )
- )
- [message:protected] => Internal Server Error
- [string:Exception:private] =>
- [code:protected] => 500
- [file:protected] =>PATH.TO.FILEshopifyshopify.php
- [line:protected] => 28
- [trace:Exception:private] => Array
- (
- [0] => Array
- (
- [file] => PATH.TO.FILEindex.php
- [line] => 44
- [function] => {closure}
- [args] => Array
- (
- [0] => POST
- [1] => /admin/custom_collections.json
- [2] => Array
- (
- [custom_collection] => Array
- (
- [title] => IPods
- [collects] => Array
- (
- [product_id] => 99395362
- )
- )
- )
- [3] => Array
- (
- [http_status_message] => Internal Server Error
- [http_status_code] => 500
- [server] => nginx
- [date] => Sat, 15 Sep 2012 18:56:36 GMT
- [content-type] => application/json; charset=utf-8
- [transfer-encoding] => chunked
- [connection] => keep-alive
- [status] => 500 Internal Server Error
- [x-shopify-shop-api-call-limit] => 1/500
- [http_x_shopify_shop_api_call_limit] => 1/500
- [cache-control] => no-cache
- [x-request-id] => f6fdb72b6cabf22a73207d82b8ea16d9
- [x-ua-compatible] => IE=Edge,chrome=1
- [set-cookie] => _secure_session_id=cf2648422369ee231220b6f1f1efdb3b; path=/; secure; HttpOnly
- [x-runtime] => 0.047004
- )
- )
- )
- )
- [previous:Exception:private] =>
- )
- $url = YOUR_SOPIFY_URL_WHERE_YOU_WANT_TO_POST.
- $username = "username";
- $password = "password";
- $product = array();
- $ch = curl_init();
- $opts = array(
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => 1,
- CURLOPT_POST => 1,
- CURLOPT_POSTFIELDS => array('data' => json_encode($product)),
- CURLOPT_TIMEOUT => 120,
- CURLOPT_FAILONERROR => 1,
- CURLOPT_HTTPAUTH => CURLAUTH_ANY,
- CURLOPT_USERPWD => "$username:$password"
- );
- curl_setopt_array($ch, $opts);
- $response = curl_exec($ch);
- curl_close($ch);
- return json_decode($response);
Add Comment
Please, Sign In to add comment