在进行Web应用程序开发的时候,人们经常会用Session存储数据。但可能有人不知道,在PHP中,Session使用不当可能会引起并发问题。印度医疗行业软件解决方案提供商Plus91 Technologies高级工程师Kishan Gor在个人博客上对这个问题进行了阐释。 如果同一个客户端并发发送多个请求,而每个请求都使用了Session,那么PHP Session锁的存在会导致服务器串行响应这些请求,而不是并行。这是因为在默认情况下,PHP使用文件存储Session数据。对于每一个新的 Session,PHP会创建一个文件,并持续向其中写入数据。所以,每次调用session_start()方法,就会打开Session文件,并取得 文件的独占锁。这样,如果服务器脚本正在处理一个请求,而客户端又发送了一个同样需要使用Session的请求,那么后一个请求会阻塞,直至前一个请求处 理完成释放了文件上的独占锁。不过,这只限于来自同一个客户端的多个请求,也就是说,来自一个客户端的请求并不会阻塞另一个客户端的请求。 如果脚本很短,这通常没有问题。但如果脚本运行时间比较长,那就可能会产生问题。在现代Web应用程序开发中,有一个非常常见的情况,就是使用…
PHP API:bigcommerce-api-php, php-shopify
1. Bigcommerce API Client
PHP client for connecting to the Bigcommerce V2 REST API.
To find out more, visit the official documentation website: http://developer.bigcommerce.com/
项目地址:https://github.com/bigcommerce/bigcommerce-api-php
下载:https://github.com/bigcommerce/bigcommerce-api-php/archive/master.zip
克隆:https://github.com/bigcommerce/bigcommerce-api-php.git
Requirements
- PHP 5.3 or greater
- cUrl extension enabled
To connect to the API with basic auth you need the following:
- Secure URL pointing to a Bigcommerce store
- Username of an authorized admin user of the store
- API key for the user
To generate an API key, go to Control Panel > Users > Edit User and make sure the ‘Enable the XML API?’ is ticked.
To connect to the API with OAuth you will need the following:
- client_id
- auth_token
- store_hash
Installation
Use the following Composer command to install the API client from the Bigcommerce vendor on Packagist:
$ composer require bigcommerce/api $ composer update
You can also install composer for your specific project by running the following in the library folder.
$ curl -sS https://getcomposer.org/installer | php $ php composer.phar install $ composer install
用法:
<?php use Bigcommerce\Api\Client as Bigcommerce; use Bigcommerce\Api\ClientError; use Bigcommerce\Api\ServerError; use Bigcommerce\Api\Error; include_once rtrim(dirname(__FILE__), '/').'/vendor/autoload.php'; $config = array( 'store_url' => 'https://yoursite.com', 'username' => 'yoursitename', 'api_key' => 'yoursitekey' ); Bigcommerce::configure($configs); Bigcommerce::failOnError(); // about products Bigcommerce::getProducts() Bigcommerce::getProductsCount(); $product = Bigcommerce::getProduct(11); echo $product->name; echo $product->price; //Paging and Filtering //All the default collection methods support paging, by passing the page number to the method as an integer: $products = Bigcommerce::getProducts(3); //If you require more specific numbering and paging, you can explicitly specify a limit parameter: $filter = array("page" => 3, "limit" => 30); $products = Bigcommerce::getProducts($filter); //To filter a collection, you can also pass parameters to filter by as key-value pairs: $filter = array("is_featured" => true); $featured = Bigcommerce::getProducts($filter); // about customers Bigcommerce::getCustomer($customer_id); Bigcommerce::getCustomerAddresses($customer_id); // about orders Bigcommerce::getOrders($filter); Bigcommerce::getOrdersCount($filter); Bigcommerce::getOrderShippingAddresses($order_id, $filter); Bigcommerce::getOrderShippingAddress($order_id, $order_shippment_address_id); Bigcommerce::getShipments($order_id, $filter); Bigcommerce::getShipment($order_id, $shipment_id); Bigcommerce::getOrderProducts($order_id); Bigcommerce::getOrderProductsCount($order_id, $filter);
更多方法函数,可以参看源文件: PHPBigcommerce/vendor/bigcommerce/api/src/Bigcommerce/Api/Client.php
Updating existing resources (PUT)
To update a single resource:
$product = Bigcommerce::getProduct(11); $product->name = "MacBook Air"; $product->price = 99.95; $product->update();
You can also update a resource by passing an array or stdClass object of fields you want to change to the global update method:
$fields = array( "name" => "MacBook Air", "price" => 999.95 ); Bigcommerce::updateProduct(11, $fields);
Creating new resources (POST)
Some resources support creation of new items by posting to the collection. This can be done by passing an array or stdClass object representing the new resource to the global create method:
$fields = array( "name" => "Apple" ); Bigcommerce::createBrand($fields);
You can also create a resource by making a new instance of the resource class and calling the create method once you have set the fields you want to save:
$brand = new Bigcommerce\Api\Resources\Brand(); $brand->name = "Apple"; $brand->create();
Deleting resources and collections (DELETE)
To delete a single resource you can call the delete method on the resource object:
$category = Bigcommerce::getCategory(22); $category->delete();
You can also delete resources by calling the global wrapper method:
Bigcommerce::deleteCategory(22);
Some resources support deletion of the entire collection. You can use the deleteAll methods to do this:
Bigcommerce::deleteAllOptionSets();
Using The XML API
Bigcommerce::useXml();
This will configure the API client to use XML for all subsequent requests. Note that the client does not convert XML to PHP objects. In XML mode, all object parameters to API create and update methods must be passed as strings containing valid XML, and all responses from collection and resource methods (including the ping, and count methods) will return XML strings instead of PHP objects. An example transaction using XML would look like:
Bigcommerce::useXml(); $xml = "<?xml version="1.0" encoding="UTF-8"?> <brand> <name>Apple</name> <search_keywords>computers laptops</search_keywords> </brand>"; $result = Bigcommerce::createBrand($xml);
更多:https://github.com/bigcommerce/bigcommerce-api-php
2. PHP Shopify SDK
PHPShopify is a simple SDK implementation of Shopify API. It helps accessing the API in an object oriented way.
项目地址:https://github.com/phpclassic/php-shopify
下载:https://github.com/phpclassic/php-shopify/archive/master.zip
克隆:https://github.com/phpclassic/php-shopify.git
Installation
Install with Composer
composer require phpclassic/php-shopify
Requirements
PHPShopify uses curl extension for handling http calls. So you need to have the curl extension installed and enabled with PHP.
However if you prefer to use any other available package library for handling HTTP calls, you can easily do so by modifying 1 line in each of the
get()
,post()
,put()
,delete()
methods inPHPShopify\HttpRequestJson
class.
Usage
You can use PHPShopify in a pretty simple object oriented way.
Configure ShopifySDK
If you are using your own private API, provide the ApiKey and Password.
$config = array( 'ShopUrl' => 'yourshop.myshopify.com', 'ApiKey' => '***YOUR-PRIVATE-API-KEY***', 'Password' => '***YOUR-PRIVATE-API-PASSWORD***', ); PHPShopify\ShopifySDK::config($config);
For Third party apps, use the permanent access token.
$config = array( 'ShopUrl' => 'yourshop.myshopify.com', 'AccessToken' => '***ACCESS-TOKEN-FOR-THIRD-PARTY-APP***', ); PHPShopify\ShopifySDK::config($config);
用法:
<?php include_once rtrim(dirname(__FILE__), '/').'/vendor/autoload.php'; $config = array( 'url' => 'yousrsite.com', 'key' => 'yousrsitekey', 'password' => 'yousrsitepassword', ); $shopify = new PHPShopify\ShopifySDK($configs); $shopify->Product($productID)->get(); $shopify->Product->get(); $shopify->Product->count(); $shopify->Order->get($filter); $shopify->Order->count($filter); $shopify->Customer($customer_id)->get(); // (GET request) $params = array( 'status' => 'cancelled', 'created_at_min' => '2016-06-25T16:15:47-04:00', 'fields' => 'id,line_items,name,total_price' ); $orders = $shopify->Order->get($params); // Create a new order (POST Request) $order = array ( "email" => "foo@example.com", "fulfillment_status" => "unfulfilled", "line_items" => [ [ "variant_id" => 27535413959, "quantity" => 5 ] ] ); $shopify->Order->post($order); // Update an order (PUT Request) $updateInfo = array ( "fulfillment_status" => "fulfilled", ); $shopify->Order($orderID)->put($order); // Remove a Webhook (DELETE request) $webHookID = 453487303; $shopify->Webhook($webHookID)->delete()); // get the images of a product (GET request) $productID = 23564666666; $productImages = $shopify->Product($productID)->Image->get(); // Add a new address for a customer (POST Request) $address = array( "address1" => "129 Oak St", "city" => "Ottawa", "province" => "ON", "phone" => "555-1212", "zip" => "123 ABC", "last_name" => "Lastnameson", "first_name" => "Mother", "country" => "CA", ); $customerID = 4425749127; $shopify->Customer($customerID)->Address->post($address); // Create a fulfillment event (POST request) $fulfillmentEvent = array( "status" => "in_transit" ); $shopify->Order($orderID)->Fulfillment($fulfillmentID)->Event->post($fulfillmentEvent); // Update a Blog article (PUT request) $blogID = 23564666666; $articleID = 125336666; $updateArtilceInfo = array( "title" => "My new Title", "author" => "Your name", "tags" => "Tags, Will Be, Updated", "body_html" => "<p>Look, I can even update through a web service.<\/p>", ); $shopify->Blog($blogID)->Article($articleID)->put($updateArtilceInfo); // Delete any specific article from a specific blog (DELETE request) $blogArticle = $shopify->Blog($blogID)->Article($articleID)->delete(); // Custom Actions - get count of total projects $productCount = $shopify->Product->count(); // Custom Actions - Make an address default for the customer. $shopify->Customer($customerID)->Address($addressID)->makeDefault(); // Custom Actions - Search for customers with keyword "Bob" living in country "United States". $shopify->Customer->search("Bob country:United States");
更多参看:https://github.com/phpclassic/php-shopify
本文:PHP API:bigcommerce-api-php, php-shopify