json - how to use shopify api php
Hey i am new in shopify and i have create new private app in my store.i want to use shopify api in my php program.i have write following code in my php file.
Code:
<?php
$API_KEY = 'XXX';
$SECRET = 'XXX';
$STORE_URL = 'XXX';
$PRODUCT_ID = 'XXX';
$url = 'https://' . $API_KEY . ':' . md5($SECRET) . '@' . $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);
$response = curl_exec($session);
curl_close($session);
$product_xml = new SimpleXMLElement($response);
echo $product_xml->title;
echo $product_xml->variants->variant->{'inventory-quantity'};
?>
i got the following error.
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in index.php on line 24
Exception: String could not be parsed as XML in C:\wamp\www\apis\index.php on line 24
please help me to short out this problem as soon as possible.please please... thanks in advance.
Answer
Solution:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$data = json_decode($data );
Use the above code.
Shopify returns the data in the JSON format, so decode it and use it for your purpose.
Also make sure that the URL that you are using is correct by loading it in the browser.
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.