vulcanphp/easycurl

Easy Curl 是一个PHP的助手类,使HTTP请求变得更容易

v1.0.0 2024-05-24 20:40 UTC

This package is auto-updated.

Last update: 2024-09-24 21:24:18 UTC


README

Easy Curl 是一个PHP的助手类,使HTTP请求变得更容易

安装

建议您使用 Composer 安装PHP Easy Curl

$ composer require vulcanphp/easycurl

PHP Easy Curl 基本用法

<?php
// index.php

use VulcanPhp\EasyCurl\EasyCurl;

require_once __DIR__ . '/vendor/autoload.php';

// make a http get, post, put, patch, and delete request
var_dump(
    EasyCurl::get('https://jsonplaceholder.typicode.com/posts')
        ->getJson()
);

// can make every http request statically
var_dump(
    EasyCurl::put('https://jsonplaceholder.typicode.com/posts/1')
        ->getBody()
);

// create a Easy Curl instance
$http = EasyCurl::create();

// set curl options
$http->setOption(CURLOPT_TIMEOUT, 20);

// set header
$http->setHeader('Authorization', 'Bearer {token}');

// set postfields 
$http->setPostFields(['name' => 'John Doe', 'email' => 'john@mail.com']);

// send this request
$resp = $http->post('http://your-location.domain/path');

// print the response
var_dump($resp);

// ...

PHP Easy Curl 响应用法

<?php
// index.php
use VulcanPhp\EasyCurl\EasyCurl;

require_once __DIR__ . '/vendor/autoload.php';

$resp = EasyCurl::get('https://jsonplaceholder.typicode.com/posts');

// get the curl output in array
var_dump($resp->getResponse());

// get the curl status
var_dump($resp->getStatus());

// get response body
var_dump($resp->getBody());

// if the output is json format we can convert it with array
var_dump($resp->getJson()); 

// get output content length
var_dump($resp->getLength());

// get curl last effective url
var_dump($resp->getLastUrl());

// Tip: all methods can be called without get word, EX: $resp->body() is equal to $resp->getBody()

快速用法

<?php
// index.php

use VulcanPhp\EasyCurl\EasyCurl;

require_once __DIR__ . '/vendor/autoload.php';

// quick create a curl instance with static/non-static methods
$http = EasyCurl::options([
    CURLOPT_TIMEOUT => 20,
    // all curl_setopt() options 
]);

// send method is equal to get method, or you can use any http methods
$resp = $http->send('http://domain.com/path');

// download a file
$status = EasyCurl::downloadFile(__DIR__ . '/test.txt')
    ->get('http://domain.com/path')
    ->status();

var_dump($status);

// setup a proxy in easy curl
$http = EasyCurl::proxy([
    'ip'    => '127.0.0.1', // proxy ip
    'port'  => '8080', // proxy port
    'auth'  => 'user:pass', // basic proxy auth
]);

var_dump($http->get('http://domain.com/path'));

// ...

EasyCurl 驱动程序可用的方法

  • send(string $url, array $params = []): ICurlResponse
  • get(string $url, array $params = []): ICurlResponse
  • post(string $url, array $params = []): ICurlResponse
  • put(string $url, array $params = []): ICurlResponse
  • patch(string $url, array $params = []): ICurlResponse
  • delete(string $url, array $params = []): ICurlResponse
  • setOption(int $key, mixed $value): ICurlDriver
  • setOptions(array $options): ICurlDriver
  • setUseragent(string $useragent): ICurlDriver
  • setHeader(string $key, string $value): ICurlDriver
  • setHeaders(array $headers): ICurlDriver
  • setCookieFile(string $filepath): ICurlDriver
  • setDownloadFile(string $filepath, bool $override = false): ICurlDriver
  • setPostFields(mixed $fields): ICurlDriver
  • setProxy(array $proxy): ICurlDriver

注意:创建 Easy Curl 实例时,可以静态调用这些方法。

并且也可以不使用(set)这个词调用每个设置方法,例如:EasyCurl::option() 等同于 EasyCurl::setOption()