shopify 接口開發
shopify簡介
Shopify是由托比亞斯·盧克創辦的加拿大電子商務軟件開發商,總部位於加拿大首都渥太華,其提供的服務軟件Shopify是一個SaaS領域的購物車系統,適合跨境電商建立獨立站,用戶支付一定費用即可在其上利用各種主題/模板建立自己的網上商店。
文檔地址
授權
OAuth
使用接口
REST Admin API reference
//範例語言
PHP
$shop='xxx';//店鋪名,指的是後台登陸地址欄內xxx.myshopify.com 這個xxx$api_key="xxx";//app key 在開發者平台中創建app就會生成//需要獲取的權限$scopes="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";//返回地址這個可以任意填寫$redirect_uri="";//構建請求地址$install_url="https://".$shop.".myshopify.com/admin/oauth/authorize?client_id=".$api_key."&scope=".$scopes."&redirect_uri=".urlencode($redirect_uri);//請求地址生成後可直接使用GET 方式發送//進入shopify授權界面,此時輸入用戶名密碼,後點擊授權//返回地址上會有code=xxxx//接下來獲取token值//構建請求訊息$request_headers=array("Content-type"=>"application/json",// 告訴Shopify我們期待JSON格式的回复"client_id"=>"daeaac41584625af84a5707f708336f6",// 創建app時產生"client_secret"=>"1f73927bbefec664c142fd22e118de59",// 創建app時產生"code"=>$code// 上面返回的code值);//發送請求,獲得返回值$shopify_response=$this->shopify_call(NULL,$shop,"/admin/oauth/access_token",$request_headers,'POST',$request_headers);//解析json字符串$shopify_response=json_decode($shopify_response['response'],TRUE);//範例//訂單接口$create_time_from_date=date('Ym-d',$create_time_from).'T'.date('H:i:s',$create_time_from);$opt=array("limit"=>200,"page"=>$page,"financial_status"=>'paid',"updated_at_min"=>$create_time_from_date,);$orders=$this->shopify_call($token,$shop,"/admin/orders.json",$opt,'GET');//產品創建接口$opt=array();$insert=array();$insert['title']='';$insert['body_html']='';$insert['vendor']='';$insert['product_type']='';$insert['tags']='';//還有可以添加很多參數$opt['product']=$insert;$product=$this->shopify_call($token,$shop,"/admin/api/2019-07/products.json",$opt,'POST',array(0=>'Content-Type: application/json'));//使用的基礎函數//$token 上方獲取的token值//$shop 同上方相同//$api_endpoint 接口路徑//$query 接口參數//$method 訪問方式//$request_headers 頭部訊息functionshopify_call($token,$shop,$api_endpoint,$query=array(),$method='GET',$request_headers=array()){// Build URL$url="https://".$shop.".myshopify.com".$api_endpoint;if(!is_null($query)&&in_array($method,array('GET','DELETE')))$url=$url."?".http_build_query($query);// Configure cURL$curl=curl_init($url);curl_setopt($curl,CURLOPT_HEADER,TRUE);curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);curl_setopt($curl,CURLOPT_FOLLOWLOCATION,TRUE);curl_setopt($curl,CURLOPT_MAXREDIRS,3);curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);// curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 3);// curl_setopt($curl, CURLOPT_SSLVERSION, 3);curl_setopt($curl,CURLOPT_USERAGENT,'My New Shopify App v.1');curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,30);curl_setopt($curl,CURLOPT_TIMEOUT,30);curl_setopt($curl,CURLOPT_CUSTOMREQUEST,$method);// Setup headers$request_headers[]="";if(!is_null($token))$request_headers[]="X-Shopify-Access-Token: ".$token;curl_setopt($curl,CURLOPT_HTTPHEADER,$request_headers);if($method!='GET'&&in_array($method,array('POST','PUT'))){if(is_array($query)){$fquery=http_build_query($query);if($request_headers[0]=='Content-Type: application/json')$fquery=json_encode($query);}curl_setopt($curl,CURLOPT_POSTFIELDS,$fquery);}// Send request to Shopify and capture any errors$response=curl_exec($curl);$response=str_replace("HTTP/1.1 100 Continuerr",'',$response);$response=str_replace("HTTP/1.1 100 Continuernrn",'',$response);$response=str_replace("HTTP/1.1 100 Continuenn",'',$response);$error_number=curl_errno($curl);$error_message=curl_error($curl);// Close cURL to be nicecurl_close($curl);// Return an error is cURL has a problemif($error_number){return$error_message;}else{// No error, return Shopify's response by parsing out the body and the headers$response=preg_split("/rnrn|nn|rr/",$response,2);// Convert headers into an array$headers=array();$header_data=explode("n",$response[0]);$headers['status']=$header_data[0];// Does not contain a key, have to explicitly setarray_shift($header_data);// Remove status, we've already set it aboveforeach($header_dataas$part){$h=explode(":",$part);$headers[trim($h[0])]=trim($h[1]);}// Return headers and Shopify's responsereturnarray('headers'=>$headers,'response'=>$response[1]);}}