lliure/llrequest

一个轻量级的PHP包,用于连接RESTful HTTP API和SOAP服务。

v1.2.3 2024-04-24 16:01 UTC

This package is auto-updated.

Last update: 2024-08-24 16:45:09 UTC


README

概述

llRequest 是一个多功能的PHP库,用于发送SOAP和HTTP API请求。它通过提供一个易于使用的接口来简化与远程服务的交互过程,用于发送请求和处理响应。

安装

要使用 llRequest,您可以通过Composer将其包含在项目中。将以下行添加到您的 composer.json 文件中

"require": {
    "ll/request": "^1.0"
}

然后运行 composer install 以安装该包。

示例

SOAP请求

use ll\Request\Request;

// Set SOAP headers
$headers = [
    'login' => 'xxxx',
    'password' => 'xxxx',
    'trace' => 1,
    'exception' => 0
];

// Make a SOAP request
Request::soap('http://xxxxx/service.svc?wsdl')
    ->headers($headers)
    ->ProductGet(['idProduct' => 4])
    ->done(function ($response) {
        echo '<pre>' , __LINE__ , ": " , print_r($response, true) , '</pre>';
    });

HTTP/API GET请求

use ll\Request\Request;

// Make an HTTP GET request
Request::http('http://xxxxx.xxx/xxx/xxx')
    ->headers([
        'Content-Type' => 'application/json'
    ])
    ->get()
    ->done(function ($response) {
        echo '<pre>' , __LINE__ , ": " , print_r($response->meta(), true) , '</pre>';
        $decodedResponse = json_decode($response);
        echo '<pre>' , __LINE__ , ": " , print_r($decodedResponse, true) , '</pre>';
    });

HTTP/API POST请求

use ll\Request\Request;

// Make an HTTP POST request
Request::http('http://xxxxx.xxx/xxx/xxx')
    ->headers([
        'Content-Type' => 'application/json'
    ])
    ->post(json_encode(['name' => 'tintim']))
    ->done(function ($response) {
        echo '<pre>' , __LINE__ , ": " , print_r($response->meta(), true) , '</pre>';
        $decodedResponse = json_decode($response);
        echo '<pre>' , __LINE__ , ": " , print_r($decodedResponse, true) , '</pre>';
    });

用法

  • SOAP请求:使用 Request::soap($wsdlUrl) 来启动SOAP请求。您可以使用 ->headers($headersArray) 来设置头信息,并使用 ->MethodName($paramsArray) 来调用方法。
  • HTTP/API请求:使用 Request::http($url) 来启动HTTP请求。使用 ->headers($headersArray) 来设置头信息,并指定请求类型(->get()->post($data) 等)。使用 ->done() 方法来处理响应。

注意

  • 请确保通过Composer安装了所需的依赖项(ll/request)。
  • 将占位符值(xxxxhttp://xxxxx.xxx/xxx/xxx 等)替换为与您的用例相关的实际URL、凭据和数据。
  • ->done() 回调函数内处理响应数据,按您的应用需要进行处理。

有关更多信息和使用说明的详细信息,请参阅官方 llRequest 文档或源代码库。