chilimatic/openxclient

ZF-REST客户端

dev-master 2016-04-18 17:17 UTC

This package is auto-updated.

Last update: 2024-09-09 13:27:16 UTC


README

此库提供了一个客户端类,包含示例代码,以方便访问OpenX API。文件OX3_Api_Client.php支持Zend Framework版本1,文件OX3_Api_Client2.php支持Zend Framework版本2。

#安装 如果使用Zend Framework 1:

  1. 安装Zend Framework 1.12.13(链接:http://framework.zend.com/downloads/latest#ZF2)。
  2. 设置include_path
  • 方法1:导航到/private/etc/php.ini,将include_path设置为"/library/"目录下Zend Framework的路径。
include_path = ".:/Users/.../ZendFramework-1.12.13/library/"   
  • 方法2:或者,您可以创建一个名为'set_path.php'的文件,并在其中设置路径。如果您选择使用此选项设置路径,则set_path.php文件应如下所示
<?php  
$path1 = '/Users/.../ZendFramework-1.12.13/library/';  

set_include_path($path1);  
?>  

** 如果您使用此选项,请确保在包含OX3_API_Client.php文件的文件夹中创建文件。还请注意,在OX3_Api_Client.php文件的开头添加"require_once 'set_path.php'; "来包含文件。

如果使用Zend Framework 2

  1. 安装Zend Framework 2(链接:http://framework.zend.com/downloads/latest#ZF2)。
  2. 在您的工作目录中安装Composer(链接:https://getcomposer.org.cn/doc/00-intro.md)。
  3. 运行命令"php composer.phar init"。完成此操作后,Composer将引导您通过配置composer.json文件的流程。要使用默认配置,按Enter键或"yes"。
  4. 安装所需的Zend包,ZendOAuth和ZendRest
    为此,请将"repositories"和"require"部分添加到composer.json文件中,使文件如下所示
    如下所示
{    
"name": "",  
"authors": [
{
"name": "",
"email": ""
}
],

"repositories": [
{
"type": "composer",
"url": "https://packages.zendframework.com/"
}
],
"require": {
"zendframework/zendoauth": "2.0.*", 
"zendframework/zendrest": "2.0.*" 
}
}
  1. 运行命令"php composer.phar install"以安装包及其依赖项。
  2. 设置include_path
  • 方法1:导航到/private/etc/php.ini,将include_path设置为"/library/"目录下Zend Framework的路径以及"/vendor/"目录下新安装的包的路径。在安装包后,"vendor"文件夹应出现在您的工作目录中。
include_path = ".:/Users/.../ZendFramework-2.4.5/library/:/Users/.../vendor/" 
  • 方法2:或者,您可以创建一个名为'set_path.php'的文件,并在其中设置路径。如果您选择使用此选项设置路径,则set_path.php文件应如下所示
<?php  
$path1 = '/Users/.../ZendFramework-2.4.5/library/';
$path2 = '/Users/.../vendor/';  

set_include_path($path1 . PATH_SEPARATOR . $path2);  
?>  

** 如果您使用此选项,请确保在包含OX3_Api_Client2.php文件的文件夹中创建文件。还请注意,在OX3_Api_Client2.php文件的开头添加"require_once 'set_path.php'; "来包含文件。

#认证/示例脚本
将此代码添加到您的代码中以进行Oauth认证

<?php
// If using Zend Framework 1
require_once 'OX3_Api_Client.php';
// if Using Zend Framework 2
require_once 'OX3_Api_Client2.php';

$uri      = 'http://host';  
$email    = 'root@openx.org';  
$password = '';  
$key      = '';  
$secret   = '';  
$realm    = '';  

// If using Zend Framework 1
$client = new OX3_API_Client($uri, $email, $password, $key, $secret, $realm); 
// if Using Zend Framework 2
$client = new OX3_API_Client2($uri, $email, $password, $key, $secret, $realm); 
?>

** 注意:当运行示例脚本时,OX3_Api_Client.php/OX3_Api_Client2.php必须与脚本在同一个文件夹中。还请注意,示例脚本中包含一些用户可配置的变量(除了认证部分),这些变量在脚本顶部有描述。

#使用方法

  • 要在命令行中以友好的格式查看结果,请使用函数json_Decode、getBody和print_r。例如:$result = $client->get('/account');
    print_r(json_decode($result->getBody(), true));

GET请求

  • 要获取特定类型的所有当前对象,请使用以下请求
$result = $client->get('/"object_type"');  
Ex) $result = $client->get('/account');  
  • 要获取具有特定属性值的对象(s),请传入一个包含路径的数组以及所需属性值的值
$query = array("attribute"=>"value");  
$result = $client->get('/object_type', $query')  
Ex) $query1 = array('name'=>'OpenX');  
result1 = $client->get('/account', $query1);  
--> Returns the account(s) with the name OpenX  
  • 许多字段有多个选项可以取值。要查看这些选项,请使用以下请求
$result = $client->get('/options/"field_options"')  
Ex) $content_types = $client->get('/options/content_type_options');

POST请求

  • 创建对象,请发送一个POST请求,包含路径和包含对象字段值的数组
$query = array(  
'account_uid'=>"...",   
'currency'=>"...",   
.  
.  
.  
'timezone'=>"...");  
$result = $client->post('/"object_type"/', $query);    

PUT 请求

  • 更新对象,请发送一个PUT请求,包含路径和包含要更新的参数的数组
$query = array('timezone'=>'"updated_value"');  
$result = $client->put('/"object_type"/"object_uid"', $query);   

DELETE 请求

  • 删除对象,请发送一个DELETE请求,包含要删除的对象的路径以及uid/id
$result = $client->delete('/"object_type"/"object_uid"');  
Ex) $result = $client->delete('/site/6003a1c2-e000-fff1-8123-0c9a66');