Elizabeth
Engineer
Engineer
  • UID625
  • Fans1
  • Follows1
  • Posts68
Reads:1386Replies:0

PHP SDK

Created#
More Posted time:Aug 3, 2016 14:11 PM
PHP SDK


Create Access Key


Steps:


1.Log into an Alibaba Cloud account.
2.Open the "My Access Key" page, URL: http://i.aliyun.com/access_key/
3.Create and manage your Access Key




Install the PHP SDK


Supported Development Environments:
Alibaba Cloud's PHP SDK supports PHP version 5 and above.


Installation Steps:
Decompress the package to any directory in the system (ensure that you have the read permission to the directory) and include its header file in your PHP file (ensure that the file path is correct)


Then you can use the SDK.


Quick Start


Initializing an AliyunClient


The files associated with the Alibaba Cloud SDK are all in the "aliyun" folder;


The AliyunClient is the interface for interacting with APIs. SDK operations are all completed through the AliyunClient;
The AliyunClient can be reused. We recommend setting it unique throughout the application;
Users can modify the variables "$connectTimeout" and "$readTimeout" in the class "AliyunClient" to set the SDK call interface's connection timeout and read timeout. The SDK's default connection timeout is 3 seconds and the default read timeout is 80 seconds.


Sample Code


<?php
   require_once 'TopSdk.php';


   $c = new AliyunClient;
   $c->accessKeyId = "<accessKeyId>";
   $c->accessKeySecret = "<accessKeySecret>";
   $c->serverUrl = "<serverUrl>"; // Select an appropriate domain name according to the actual product type, for example: CDN, http://cdn.aliyuncs.com/
?>


SDK call example


Call steps:


1.Instantiate a request class. The corresponding request classes will be instantiated based on different called API versions;
2.Assign values to request instances;
3.Call the interface to return call results.


Package name descriptions: The following uses the CDN API 2014-11-11 version as an example. CDN is the product name and 20141111 is the called API version.


Request class namespace:Cdn20141111OpenCdnRequest


Sample code:


Activate the CDN Service


//Activate the CDN Service
$req = new Cdn20141111OpenCdnRequest();
$req->setInternetChargeType("PayByTraffic"); // or PayByBandwidth


try {
   $resp = $c->execute($req);
   if(!isset($resp->Code))
   {
       //Activation Successful
       echo($resp->RequestId);
       print_r($resp);
   }
   else
   {
       //Activation Failed
       $code = $resp->Code;
       $message = $resp->Message;
   }
}
catch (Exception $e)
{
   // TODO: handle exception
}


Refresh Cache


// Refresh Cache
$req = new Cdn20141111RefreshObjectCachesRequest();
$req->setObjectType("File"); // or Directory
$req->setObjectPath("www.yourdomain.com/path/filename.ext");
try {
   $resp = $c->execute($req);
   if(!isset($resp->Code))
   {    
       //Refresh Successful
       echo($resp->RequestId);
       print_r($resp);
   }
   else
   {
       //Refresh Failed
       $code = $resp->Code;
       $message = $resp->Message;
   }
}
catch (Exception $e)
{
   // TODO: handle exception
}
Guest