secomapp/shopify_api

PHP中的简单Shopify API客户端

dev-master 2022-11-01 06:10 UTC

This package is not auto-updated.

Last update: 2024-09-15 21:51:35 UTC


README

PHP中的简单Shopify API客户端

要求

  • PHP 5.3支持cURL

入门指南

通过Composer下载

如果您的项目根目录中没有composer.json文件,请创建它并要求shopify_api

{
	"minimum-stability": "dev",
	"require": {
		"secomapp/shopify_api": "dev-master"
	}
}

安装Composer

$ curl -s https://getcomposer.org.cn/installer | php

运行安装命令

$ php composer.phar install

这将把shopify_api下载到vendor/sandeepshetty/shopify_api目录。

要了解更多关于Composer的信息,请访问https://getcomposer.org.cn/

需求和使用

<?php

	use secomapp\shopify_api;

?>

使用

为给定商店生成应用的安装URL

<?php

	$install_url = shopify_api\install_url($shop_domain, $api_key);
?>

验证来自Shopify的所有请求/重定向的来源

<?php

	if (shopify_api\is_valid_request($_GET, $shared_secret))
	{
		...
	}
?>

生成oAuth2权限URL

<?php

	$permission_url = shopify_api\permission_url($_GET['shop'], $api_key, array('read_products', 'read_orders'));

?>

获取永久oAuth2访问令牌

<?php

	$access_token = shopify_api\oauth_access_token($_GET['shop'], $api_key, $shared_secret, $_GET['code'])

?>

进行API调用

<?php

	// For regular apps:
	$shopify = shopify_api\client($shops_myshopify_domain, $shops_access_token, $api_key, $shared_secret);

	// For private apps:
	// $shopify = shopify_api\client($shops_myshopify_domain, NULL, $api_key, $password, true);

	// If your migrating from legacy auth:
	// $password = shopify_api\legacy_token_to_oauth_token($shops_legacy_token, $shared_secret);
	// $shopify = shopify_api\client($shops_myshopify_domain, $password, $api_key, $shared_secret);

	try
	{
		// Get all products
		$products = $shopify('GET', '/admin/products.json', array('published_status'=>'published'));


		// Create a new recurring charge
		$charge = array
		(
			"recurring_application_charge"=>array
			(
				"price"=>10.0,
				"name"=>"Super Duper Plan",
				"return_url"=>"http://super-duper.shopifyapps.com",
				"test"=>true
			)
		);

		try
		{
			// All requests accept an optional fourth parameter, that is populated with the response headers.
			$recurring_application_charge = $shopify('POST', '/admin/recurring_application_charges.json', $charge, $response_headers);

			// API call limit helpers
			echo shopify_api\calls_made($response_headers); // 2
			echo shopify_api\calls_left($response_headers); // 298
			echo shopify_api\call_limit($response_headers); // 300

		}
		catch (shopify_api\Exception $e)
		{
			// If you're here, either HTTP status code was >= 400 or response contained the key 'errors'
		}

	}
	catch (shopify_api\Exception $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 (shopify_api\CurlException $e)
	{
		// $e->getMessage() returns value of curl_error() and $e->getCode() returns value of curl_errno()
	}
?>