简体   繁体   English

如何使用shopify api php

[英]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. 嘿,我是shopify的新手,我在store中创建了新的私人应用程序。我想在我的php程序中使用shopify api。我在php文件中编写了以下代码。

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 致命错误:第24行的index.php中未捕获的异常“ Exception”,消息为“ String无法解析为XML”

Exception: String could not be parsed as XML in C:\\wamp\\www\\apis\\index.php on line 24 例外:第24行的C:\\ wamp \\ www \\ apis \\ index.php中无法将字符串解析为XML

please help me to short out this problem as soon as possible.please please... thanks in advance. 请帮助我尽快解决这个问题。请...预先表示感谢。

$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. Shopify以JSON格式返回数据,因此将其解码并用于您的目的。

Also make sure that the URL that you are using is correct by loading it in the browser. 另外,通过将其加载到浏览器中来确保所使用的URL是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM