ropendev/curl

PHP POO cURL 封装器(PSR 兼容): 轻量级且简单。

1.0.3 2019-01-03 15:15 UTC

This package is auto-updated.

Last update: 2024-09-08 07:32:52 UTC


README

使用 PHP cURL 请求 URL(或少数)变得简单

PHP POO cURL 封装器

  • PSR 兼容,
  • 使用 composer 简单安装,
  • 直观且有文档
  • 轻量级 (~8 ko)

简单的 cURL 类,将过程式默认 cURL 选项转换为对象。此类提供一些快捷方式,如 getCookie、setMobileUserAgent、setReferrer 等,以实现更直观的使用。您还可以使用 CurlRequest::setOpt($option, $value) 以旧方式设置 cURL 的选项。

所有函数都在类文件中有文档说明。

安装

通过 Packagist

composer require ropendev/curl

使用方法

单个请求

<?php
use \rOpenDev\curl\CurlRequest;

$request = new CurlRequest('http://www.example.org/');
$request->setDefaultGetOptions()->setReturnHeader()->setDestkopUserAgent()->setEncodingGzip();
$output = $curl->execute();

echo $output;

上面的示例将输出 example.org 的内容。

带有代理、POST 和 Cookie 的单个请求

<?php
use \rOpenDev\curl\CurlRequest;

$request = new CurlRequest('http://www.bing.com/');
$request->setDefaultGetOptions()->setReturnHeader()->setDestkopUserAgent()->setEncodingGzip()->execute();

$r2 = new CurlRequest('http://www.bing.com/search?q=curl+request+php');
echo $r2->setDefaultGetOptions()->setReturnHeader()->setDestkopUserAgent()->setEncodingGzip()->setCookie($request->getCookie())->setProxy('domain:port:user:password')->execute();

文档

列出所有公共方法。

<?php
use rOpenDev\curl\CurlRequest;

$r = new CurlRequest('scheme://host/path');
$r
    ->setOpt(CURLOPT_*, mixed 'value')

	// Preselect Options to avoid eternity wait
    ->setDefaultGetOptions($connectTimeOut = 5, $timeOut = 10, $dnsCacheTimeOut = 600, $followLocation = true, $maxRedirs = 5)
    ->setDefaultSpeedOptions()

    ->setReturnHeader() // If you want to get the header with getHeader()
    ->setCookie(string $cookie)
    ->setReferer(string $url)
    ->setUserAgent(string $ua)
        ->setDestkopUserAgent()
        ->setMobileUserAgent()
        ->setLessJsUserAgent()

    ->setPost(array $post)

    ->setEncodingGzip()

    ->setProxy(string '[scheme]proxy-host:port[:username:passwrd]') // Scheme, username and passwrd are facultatives. Default Scheme is http://

    ->setReturnHeaderOnly()

    ->setUrl($url, $resetPreviousOptions)

string $r->execute();   // Return contents from the url

array  $r->getHeader($arrayFormatted = true); // Return Response Header in an array (or in a string if $arrayFormatted is set to false)

string $r->getCookies();

string $r->getEffectiveUrl();

$r->hasError|getError|getInfo(); // Equivalent to curl function curl_errno|curl_error|curl_getinfo();

许可证

MIT(有关详细信息,请参阅 LICENSE 文件)

贡献者

在提交 pull request 之前,请检查测试是否仍然通过(phpunit)。

Latest Version Software License Build Status Quality Score Total Downloads