openx/ox3-php-api-client

该包的最新版本(1.0.0)没有提供许可信息。

OpenX平台API PHP客户端

1.0.0 2021-10-16 11:36 UTC

This package is auto-updated.

Last update: 2024-09-19 20:38:42 UTC


README

此库提供了一个客户端类以及示例,以简化对OpenX API的访问。它主要处理OpenX API使用的OAuth1登录过程和一些基本的路径操作。

src\OpenX\PlatformAPI目录中,

  • OXApiClient.php(使用修补过的Zend2)是Composer和自动加载支持的,一个适用于PHP7+的完全命名空间客户端。

如果您需要旧的方法,并只想使用/复制文件...

  • OX3_Api_Client.php支持Zend框架版本1
  • OX3ApiClient2.php支持Zend框架版本2

欢迎提供帮助

由于我们不积极维护PHP项目,任何帮助整理包和readme文档都将受到欢迎。

可能需要审查的内容

  • 更好的或更少的混合安装说明
  • 如果需要现代(composer,命名空间)或旧(脏文件复制,旧PHP)的设置支持,请告诉我们
  • API的/3.0/路径已过时,不再受支持,我们可以从代码中清理它们
  • 使包独立于框架
    • 特别是那些已过时的框架(Zend)

我们想要感谢

  • Fei Song帮助我们使这个包成为合适的composer包。

安装

(推荐)如果使用Composer和Zend2

  • 查看示例Dockerfile以获取灵感
  • 您只需将openx/ox3-php-api-client作为依赖项添加即可
  • 然后您可以在项目中使用OpenX\PlatformAPI\OXApiClient类。

在docker镜像中试用

  • docker build . -t openx-test
  • docker run -it openx-test bash
    • export OPENX_URI=http://your-ui.openx.net
    • export OPENX_EMAIL=john.doe@company.com
    • export OPENX_PASWORD="My password satisfies the sane requirements for a long p@assword!"
    • export OPENX_KEY=c0n5um3rK3y
    • export OPENX_SECRET=c0n5um3rS3cR3T
    • export OPENX_REALM="your_realm" #(通常与主机名前缀相同)
    • php example.php

旧的安装说明,需要审查

如果使用Zend框架1

  1. 安装Zend框架1.12.13(链接:http://framework.zend.com/downloads/latest#ZF2)。
  2. 设置include_path
  • 方法1:导航到/private/etc/php.ini,并将include_path设置为位于"/library"目录下的Zend框架路径。
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框架2

  1. 安装Zend框架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文件的流程。要允许默认配置,按回车键或"是"。
  4. 安装所需的Zend包,ZendOAuth和ZendRest
    要这样做,请在composer.json文件中添加"repositories"和"require"部分,使文件如下所示
    如下所示
{
  "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 设置为 "..." 目录下 Zend Framework 的路径以及新安装的软件包在 ".../vendor/" 目录下的路径。在用 composer 安装软件包后,"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';
// if using the composer autoloader with the namespaced client
require_once('vendor/autoload.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);
// if using the new class 
$client = new \OpenX\PlatformAPI\OXApiClient($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"');  
// Example:
$result = $client->get('/account');  
  • 要获取具有特定属性值的对象(或对象),请传递包含路径的数组以及所需属性值的数组
$query = array("attribute"=>"value");  
$result = $client->get('/object_type', $query)  
// Example:
$query1 = array('name'=>'OpenX');  
result1 = $client->get('/account', $query1);  
// --> Returns the account(s) with the name OpenX  
  • 许多字段有多种选项可以取值。要查看这些选项,请使用以下请求
$result = $client->get('/options/:field_options')  
// Example:
$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 请求,传递包含要删除的对象的 uids/id 的路径
$result = $client->delete('/:object_type/:object_uid');
// Example:  
$result = $client->delete('/site/6003a1c2-e000-fff1-8123-0c9a66');