承接 cleantalk/http 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cleantalk/http

最新稳定版本:1.0.1

Composer 安装命令:

composer require cleantalk/http

包简介

CleanTalk HTTP Requests library

README 文档

README

CleanTalk

Cleantalk HTTP Requests

Standalone PHP HTTP Request library

Description

This standalone library allowing you to use different HTTP-requests in easy way. It could be simply merged into your project. Please, see "Usage" to learn more.

Features

  • POST, GET HTTP-methods.
  • Multiple requests.
  • Asynchronous requests.
  • Callback functions.
  • CURL options support.
  • Any combination of the above features.

Usage

Download and include the file into your project by using PSR-0 (autoload) or directly attaching the files with "include" instruction. Use the following code to use the Request:

$request = new Cleantalk\Common\HTTP\Request();
$request_result = $request
    ->setUrl('example.com')
    ->setData(['foo' => 'bar']])
    ->setPresets(['async', 'ssl']))
    ->request();

You can reuse once created object change it URL and callback functions any time:

$another_request_result = $request
    ->setUrl($new_url)
    ->setCallback(
        function ($response_content, $requested_url){
            $processed_response_content = strreplace(
                'Hello',
                'Hey!',
                $response_content
            ); 
            
            return $processed_response_content;
        })
    ->request();

Or use a multiple async requests. This one will send asynchronous requests to example.com and exapme2.com wth custom user-agent and pass the response direct to the output buffer:

$miltiple_request = new Cleantalk\Common\HTTP\Request();
$multiple_request_result = $request
    ->setUrl(['example.com', 'example2.com'])
    ->setData(['foo' => 'bar']])
    ->setPresets(['async']))
    ->setOptions([
        CURLOPT_RETURNTRANSFER => fase,                  // CURL format is supported
        'user-agent'           => 'My custom User-Agent' // And user-friendly
        ])
    ->request();

Presets

The lib is using presets which allow you to configure its behaviour. Use 'setPresets()' method to set them.

->setPresets([
        'async',
        'get',
        'dont_follow_redirects'
    ])

May use the following presets (you can combine them in any way you want):

  • dont_follow_redirects - ignore 300-family response code and don't follow redirects
  • get_code - getting only HTTP response code
  • async - async requests. Sends request and return 'true' value. Doesn't wait for response.
  • get - makes GET-type request instead of default POST-type
  • ssl - uses SSL
  • cache - allow caching for this request
  • retry_with_socket - make another request with socket if cURL failed to retrieve data

Options

If you need to precise tune you could use 'setOptions' method:

->setOptions([
        CURLOPT_RETURNTRANSFER => fase,                  // CURL format is supported
        'user-agent'           => 'My custom User-Agent' // And user-friendly
    ])

It supports any cURL type options(learn more in cURL documentation) and the following human-friendly:

  • timeout - maximum connection duration(int)
  • sslverify - verify host (bool)
  • sslcertificates - pass your own SSL certificate (string)
  • headers - any custom headers (array of strings)
  • user-agent - custom user-agent (string)

License

This library is open-sourced software licensed under the GPLv3 license.

统计信息

  • 总下载量: 671
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2022-10-19