{"id":1766,"date":"2023-03-26T22:09:39","date_gmt":"2023-03-26T14:09:39","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1766"},"modified":"2023-04-23T21:20:58","modified_gmt":"2023-04-23T13:20:58","slug":"shopify-api-development-php","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/shopify-api-development-php\/","title":{"rendered":"Shopify\u63a5\u53e3\u5f00\u53d1\uff08PHP\uff09"},"content":{"rendered":"<h2>Shopify\u7b80\u4ecb<\/h2>\n<p>Shopify\u662f\u7531\u6258\u6bd4\u4e9a\u65af\u00b7\u5362\u514b\u521b\u529e\u7684\u52a0\u62ff\u5927\u7535\u5b50\u5546\u52a1\u8f6f\u4ef6\u5f00\u53d1\u5546\uff0c\u603b\u90e8\u4f4d\u4e8e\u52a0\u62ff\u5927\u9996\u90fd\u6e25\u592a\u534e\uff0c\u5176\u63d0\u4f9b\u7684\u670d\u52a1\u8f6f\u4ef6Shopify\u662f\u4e00\u4e2aSaaS\u9886\u57df\u7684\u8d2d\u7269\u8f66\u7cfb\u7edf\uff0c\u9002\u5408\u8de8\u5883\u7535\u5546\u5efa\u7acb\u72ec\u7acb\u7ad9\uff0c\u7528\u6237\u652f\u4ed8\u4e00\u5b9a\u8d39\u7528\u5373\u53ef\u5728\u5176\u4e0a\u5229\u7528\u5404\u79cd\u4e3b\u9898\/\u6a21\u677f\u5efa\u7acb\u81ea\u5df1\u7684\u7f51\u4e0a\u5546\u5e97\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u6587\u6863\u5730\u5740<\/h2>\n<ul>\n<li>\u6388\u6743 OAuth: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/shopify.dev\/tutorials\/authenticate-with-oauth\">https:\/\/shopify.dev\/tutorials\/authenticate-with-oauth<\/a><\/li>\n<li>\u4f7f\u7528\u63a5\u53e3 REST Admin API reference: <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/shopify.dev\/docs\/admin-api\/rest\/reference\">https:\/\/shopify.dev\/docs\/admin-api\/rest\/reference<\/a><\/li>\n<li>API(Ruby &amp; Node.js &amp; PHP): <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/shopify.dev\/tools\/libraries\/admin-api\">https:\/\/shopify.dev\/tools\/libraries\/admin-api<\/a><\/li>\n<\/ul>\n<h2>\u63a5\u5165\u793a\u4f8b<\/h2>\n<pre><code class=\"language-php\">$shop = &#039;xxx&#039;; \/\/\u5e97\u94fa\u540d\uff0c\u6307\u7684\u662f \u540e\u53f0\u767b\u9646\u5730\u5740\u680f\u5185 xxx.myshopify.com  \u8fd9\u4e2a xxx\n\n$api_key = &quot;xxx&quot;; \/\/app key \u5728\u5f00\u53d1\u8005\u5e73\u53f0\u4e2d\u521b\u5efaapp\u5c31\u4f1a\u751f\u6210\n\n\/\/\u9700\u8981\u83b7\u53d6\u7684\u6743\u9650\n$scopes = &quot;read_locations,read_content,write_content,read_themes,write_themes,read_products,write_products,read_product_listings,read_collection_listings,read_customers,write_customers,read_orders,write_orders,read_draft_orders,write_draft_orders,read_script_tags,write_script_tags,read_fulfillments,write_fulfillments,read_shipping,write_shipping,read_analytics,read_checkouts,write_checkouts,read_reports,write_reports,read_price_rules,write_price_rules,read_marketing_events,write_marketing_events,read_resource_feedbacks,write_resource_feedbacks,read_locations,read_inventory,write_inventory&quot;;\n\n\/\/\u8fd4\u56de\u5730\u5740 \u8fd9\u4e2a\u53ef\u4ee5\u4efb\u610f\u586b\u5199\n$redirect_uri = &quot;&quot;;\n\n\/\/\u6784\u5efa\u8bf7\u6c42\u5730\u5740\n$install_url = &quot;https:\/\/&quot; . $shop . &quot;.myshopify.com\/admin\/oauth\/authorize?client_id=&quot; . $api_key . &quot;&amp;scope=&quot; . $scopes . &quot;&amp;redirect_uri=&quot; . urlencode($redirect_uri);\n\n\/\/\u8bf7\u6c42\u5730\u5740\u751f\u6210\u540e\u53ef\u76f4\u63a5\u4f7f\u7528 GET \u65b9\u5f0f\u53d1\u9001\n\n\/\/\u8fdb\u5165shopify\u6388\u6743\u754c\u9762\uff0c\u6b64\u65f6\u8f93\u5165\u7528\u6237\u540d\u5bc6\u7801\uff0c\u540e\u70b9\u51fb\u6388\u6743\n\n\/\/\u8fd4\u56de\u5730\u5740\u4e0a\u4f1a\u6709 code=xxxx\n\n\/\/\u63a5\u4e0b\u6765\u83b7\u53d6token\u503c\n\n\/\/\u6784\u5efa\u8bf7\u6c42\u4fe1\u606f\n$request_headers = array(\n    &quot;Content-type&quot; =&gt; &quot;application\/json&quot;, \/\/ \u544a\u8bc9Shopify\u6211\u4eec\u671f\u5f85JSON\u683c\u5f0f\u7684\u56de\u590d\n    &quot;client_id&quot; =&gt; &quot;daeaac41584625af84a5707f708336f6&quot;, \/\/ \u521b\u5efaapp\u65f6\u4ea7\u751f\n    &quot;client_secret&quot; =&gt; &quot;1f73927bbefec664c142fd22e118de59&quot;, \/\/ \u521b\u5efaapp\u65f6\u4ea7\u751f\n    &quot;code&quot; =&gt; $code \/\/ \u4e0a\u9762\u8fd4\u56de\u7684code\u503c\n);\n\n\/\/\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u5f97\u8fd4\u56de\u503c\n$shopify_response = $this-&gt;shopify_call(NULL, $shop, &quot;\/admin\/oauth\/access_token&quot;, $request_headers, &#039;POST&#039;, $request_headers);\n\/\/\u89e3\u6790json\u5b57\u7b26\u4e32\n$shopify_response = json_decode($shopify_response[&#039;response&#039;], TRUE);\n\n\/\/\u793a\u4f8b\n\/\/\u8ba2\u5355\u63a5\u53e3 \n$create_time_from_date=date(&#039;Y-m-d&#039;,$create_time_from).&#039;T&#039;.date(&#039;H:i:s&#039;,$create_time_from);\n$opt=array(\n        &quot;limit&quot; =&gt; 200,\n        &quot;page&quot; =&gt; $page,\n        &quot;financial_status&quot; =&gt; &#039;paid&#039;,\n        &quot;updated_at_min&quot; =&gt; $create_time_from_date,\n);\n$orders = $this-&gt; shopify_call($token, $shop, &quot;\/admin\/orders.json&quot;, $opt, &#039;GET&#039;);\n\n\/\/\u4ea7\u54c1\u521b\u5efa\u63a5\u53e3\n$opt = array();\n$insert = array();\n$insert[&#039;title&#039;] = &#039;&#039;;\n$insert[&#039;body_html&#039;] =&#039;&#039;; \n$insert[&#039;vendor&#039;] =  &#039;&#039;;\n$insert[&#039;product_type&#039;] = &#039;&#039;;\n$insert[&#039;tags&#039;] = &#039;&#039;;\n\/\/\u8fd8\u6709\u53ef\u4ee5\u6dfb\u52a0\u5f88\u591a\u53c2\u6570\n$opt[&#039;product&#039;] = $insert;\n$product = $this-&gt;shopify_call($token,$shop,&quot;\/admin\/api\/2019-07\/products.json&quot;,$opt,&#039;POST&#039;,array(0=&gt;&#039;Content-Type: application\/json&#039;));\n\n\/\/\u4f7f\u7528\u7684\u57fa\u7840\u51fd\u6570\n\/\/$token \u4e0a\u65b9\u83b7\u53d6\u7684token\u503c\n\/\/$shop \u540c\u4e0a\u65b9\u76f8\u540c\n\/\/$api_endpoint \u63a5\u53e3\u8def\u5f84\n\/\/$query \u63a5\u53e3\u53c2\u6570\n\/\/$method \u8bbf\u95ee\u65b9\u5f0f\n\/\/$request_headers \u5934\u90e8\u4fe1\u606f\nfunction shopify_call($token, $shop, $api_endpoint, $query = array(), $method = &#039;GET&#039;, $request_headers = array()) {\n    \/\/ Build URL\n    $url = &quot;https:\/\/&quot; . $shop . &quot;.myshopify.com&quot; . $api_endpoint;\n    if (!is_null($query) &amp;&amp; in_array($method, array(&#039;GET&#039;,     &#039;DELETE&#039;))) $url = $url . &quot;?&quot; . http_build_query($query);\n    \/\/ Configure cURL\n    $curl = curl_init($url);\n    curl_setopt($curl, CURLOPT_HEADER, TRUE);\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);\n    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);\n    curl_setopt($curl, CURLOPT_MAXREDIRS, 3);\n    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);\n    \/\/ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);\n    \/\/ curl_setopt($curl, CURLOPT_SSLVERSION, 3);\n    curl_setopt($curl, CURLOPT_USERAGENT, &#039;My New Shopify App v.1&#039;);\n    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);\n    curl_setopt($curl, CURLOPT_TIMEOUT, 30);\n    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);\n    \/\/ Setup headers\n    $request_headers[] = &quot;&quot;;\n    if (!is_null($token)) $request_headers[] = &quot;X-Shopify-Access-Token: &quot; . $token;\n    curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);\n    if ($method != &#039;GET&#039; &amp;&amp; in_array($method, array(&#039;POST&#039;, &#039;PUT&#039;))) {\n        if (is_array($query)) {\n\n            $fquery = http_build_query($query);\n            if($request_headers[0]==&#039;Content-Type: application\/json&#039;)\n            $fquery=json_encode($query);\n        }\n        curl_setopt ($curl, CURLOPT_POSTFIELDS, $fquery);\n    }\n    \/\/ Send request to Shopify and capture any errors\n    $response = curl_exec($curl);\n    $response = str_replace(&quot;HTTP\/1.1 100 Continue\\r\\r&quot;,&#039;&#039;, $response);\n    $response = str_replace(&quot;HTTP\/1.1 100 Continue\\r\\n\\r\\n&quot;,&#039;&#039;, $response);\n    $response = str_replace(&quot;HTTP\/1.1 100 Continue\\n\\n&quot;,&#039;&#039;, $response);\n    $error_number = curl_errno($curl);\n    $error_message = curl_error($curl);\n    \/\/ Close cURL to be nice\n    curl_close($curl);\n    \/\/ Return an error is cURL has a problem\n    if ($error_number) {\n        return $error_message;\n    } else {\n        \/\/ No error, return Shopify&#039;s response by parsing out the body and the headers\n        $response = preg_split(&quot;\/\\r\\n\\r\\n|\\n\\n|\\r\\r\/&quot;, $response, 2);\n        \/\/ Convert headers into an array\n        $headers = array();\n        $header_data = explode(&quot;\\n&quot;,$response[0]);\n        $headers[&#039;status&#039;] = $header_data[0]; \/\/ Does not contain a key, have to explicitly set\n        array_shift($header_data); \/\/ Remove status, we&#039;ve already set it above\n        foreach($header_data as $part) {\n            $h = explode(&quot;:&quot;, $part);\n            $headers[trim($h[0])] = trim($h[1]);\n        }\n        \/\/ Return headers and Shopify&#039;s response\n        return array(&#039;headers&#039; =&gt; $headers, &#039;response&#039; =&gt; $response[1]);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Shopify\u7b80\u4ecb Shopify\u662f\u7531\u6258\u6bd4\u4e9a\u65af\u00b7\u5362\u514b\u521b\u529e\u7684\u52a0\u62ff\u5927\u7535\u5b50\u5546\u52a1\u8f6f\u4ef6\u5f00\u53d1\u5546\uff0c\u603b\u90e8\u4f4d\u4e8e\u52a0\u62ff\u5927\u9996\u90fd\u6e25\u592a\u534e\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[445],"tags":[],"class_list":["post-1766","post","type-post","status-publish","format-standard","hentry","category-shopify"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1766","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1766"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1766\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1766"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1766"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1766"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}