jabranr/php-curl

PHP cURL库快速入门

2.0.0 2018-01-27 00:27 UTC

This package is auto-updated.

Last update: 2024-09-24 04:15:07 UTC


README

一个简单的PHP客户端,用于cURL操作。

从v1迁移?注意,v2有重大变化!一些API方法名称已更改。

简单地将库导入到您的项目中。最佳做法是使用如下所示的Composer。否则,可以从GitHub下载并添加到项目中。

$ composer require jabranr/php-curl

立即开始使用。以下示例假设是通过Composer安装的。

<?php

require 'path/to/vendor/autoload.php';

use Jabran\HttpUtil\HttpCurlRequest;
use Jabran\HttpUtil\Exception\HttpCurlException;

# Start new cURL request
$curl = new HttpCurlRequest('http://jabran.me');

设置cURL选项

<?php

# Set options - method 1
$curl->setOption(CURLOPT_RETURNTRANSFER, true);
$curl->setOption(CURLOPT_FOLLOWLOCATION, true);

# Set options - method 2
$curl->setOptions(array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true
));

# Execute request, get response and close connection
try {
    $response = $curl->execute();
} catch(HttpCurlException $e) {
    $curl->getErrorCode(); // -> cURL error number
    $curl->getErrorMessage(); // -> cURL error message
    
    # OR
    
    $e->getMessage(); // -> cURL error: [ErrorCode] ErrorMessage
}

# OR get more info and close connection manually
try {
    $response = $curl->execute(false);
} catch(HttpCurlException $e) { }

# Get response later
$response = $curl->getResponse();

# 
$info = $curl->getInfo();

# Close request
$curl->close();

API

cURL类公开以下API

getInfo

获取cURL请求信息。 $option需要是一个有效的cURL常量。如果没有提供,则返回一个关联数组。

$curl->execute(false);
$curl->getInfo($option = null);
$curl->close();

getError

获取cURL请求错误消息。

$curl->getError();

getErrorCode

获取cURL请求错误代码。

$curl->getErrorCode();

getErrorMessage

获取cURL请求错误消息。

$curl->getErrorMessage();

getHttpCode

获取cURL请求HTTP状态码。

$curl->getHttpCode();

getTotalTime

获取cURL请求的总时间。

$curl->getTotalTime();

getResponse

获取cURL请求的响应。

$curl->getResponse();

许可证

请自由使用并通过拉取请求发送改进。MIT许可证下授权。

© Jabran Rafique – 2016 – 2017