phplicengine / phplicengine-api
PHPLicengine API
Requires
- php: >=5.5.0
- ext-curl: *
Suggests
- ext-fileinfo: If you want to use file upload.
- jtrumbull/xml-parser: dev-master, to use getXmlAsArray() method in Result class.
This package is auto-updated.
Last update: 2024-08-25 21:36:34 UTC
README
API
PHPLicengine API
您可以使用此 API 库满足任何需求,而不仅仅是 PHPLicengine API。使用 PHPLicengine API 库,您可以与任何 RESTApi 服务器通信,并以 json/xml 格式接收响应并进行解析。
内容
用法
您可以使用此 API 库满足任何需求,而不仅仅是 PHPLicengine API。为此,您可以直接调用 Api 类或实现自己的服务类。您可以通过调用 Api 类的 setApiKeyVar() 方法 来更改 Api 密钥头变量,以满足您的 Api 服务器的要求,以及 Result 类的 setValidResponseHeader() 方法,如果您的 Api 服务器返回带有响应 Api 头的响应,并且您需要获取它。您可以使用 [getReference() 方法] (https://github.com/phplicengine/phplicengine-api/blob/master/lib/PHPLicengine/Api/Result.php#L116) 获取它。默认情况下,这些设置是根据 PHPLicengine API 的要求设置的。
您可以直接调用 Api 类进行 PHPLicengine API,但我们为了方便,创建了您可以直接调用的服务类,例如,请参阅 Client 服务 类。
安装
版本控制与 PHPLicengine 相同。对于常规使用,您可以安装所需的任何版本。但是,如果您想使用 PHPLicengine 服务类,则应安装与您的 PHPLicengine 相同或更低的版本,如果没有相同版本的话。
composer require phplicengine/phplicengine-api x.x.x
示例
use PHPLicengine\Api\Api; $api = new Api(); // If your RESTApi server requires an Api key header, you can pass it to constructor of Api class: // $api = new Api("API key goes here."); // $api->setApiKeyVar("X-RESTAapi-Key"); // SSL verification is enabled by default. You can use below to disable it. // $api->disableSslVerification(); // You can use below to enable it again. // $api->enableSslVerification(); // timeout is set to 30 by default. You can use below to change it if needed. // $api->setTimeout(60); // Sets the host:port of a proxy to be used by cURL. If this is not set, // no proxy is used. For example, $api->setCurlProxy('proxy.example.com:3128'); // $api->setCurlProxy($proxy); // get(), post(), delete(), put(), patch() methods are available. // first parameter is url, second is query as array, third is header as array. // Only first parameter (i.e. $url) is required. $response = $api->get($url, null, null); // For debug purposes only: // print_r($api->getCurlInfo()); // print_r($response->getHeaders()); // print($response->getBody()); // exit; // for logging purposes only: // print($api->getResponse()); // print_r($api->getRequest()); // print_r($response->getHeaders()); // print($response->getBody()); if (!$api->isCurlError()) { // checks for cURL error if ($response->isOk()) { // checks for Code:200 // If your RESTApi server returns a particular header as a valid response, // you can set here: $response->setValidResponseHeader("X-ApiServer-Response"); // This checks if the valid response header above is available or not. if ($response->isValidResponse()) { if ($response->isError()) { // if response of api has error print($response->getErrorMessage()); } else { // $dataAsObject = $response->getDecodedJson(); // echo $dataAsObject->username; // echo $response->getContentType(); // You can get X-ApiServer-Response header value like below: // echo $response->getReference(); print("<pre>"); print_r($response->getJsonAsArray()); } } else { print("Invalid server response."); } } else { // response code is not 200:Ok die("Error ".$response->getResponseCode()." : ".$response->getReasonPhrase()); } } else { // api curl Error occurs. die("Curl Connection: ".$api->getCurlErrno()." : ".$api->getCurlError()); }
注意:通常,如果发生错误,RESTApi 服务器会以带有 'error' 和 'message' 元素的 json 响应返回。如果您的 RESTApi 服务器返回其他格式,您需要自定义 Result 类中的 isError() 和 getErrorMessage() 方法。
手册
自定义 cURL 选项
如果您需要添加默认未启用的 CURLOPT_* 常量,可以调用 setCurlCallback() 方法来添加它们。
use PHPLicengine\Api\Api; $api = new Api(); $api->setCurlCallback(function($ch, $params, $headers, $method) { curl_setopt($ch, CURLOPT_USERAGENT, 'some agent'); curl_setopt($ch, CURLOPT_COOKIE, 'foo=bar'); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, "username:password"); }); $api->post($url, $params, $headers);
上传文件
您可以使用 POST 方法通过此数组结构作为 post 参数上传文件。请注意,'filename' 必须是文件的绝对路径。
Array ( [files] => Array ( [0] => Array ( [filename] => /path/to/file1.txt ) [1] => Array ( [filename] => /path/to/file2.txt ) ) [foo] => bar )
服务类
有关服务类的使用方法,请参阅 此处。
变更日志
新方法:(v2.x.x)
// for logging purposes only: print($api->getResponse()); print_r($api->getRequest()); // Sets the host:port of a proxy to be used by cURL. If this is not set, // no proxy is used. For example, $api->setCurlProxy('proxy.example.com:3128'); $api->setCurlProxy($proxy);
新方法:(v2.2.2)
添加了 patch()
方法。
许可
PHPLicengine Api 在 Apache 许可证下分发。请参阅 许可证。