Code Monkey home page Code Monkey logo

php-curl's Introduction

curl

面向对象风格的Curl操作库,版本要求:PHP >= 5.3

使用示例

初始化

$curl = new Curl();
Curl::instance();

发送get请求

$curl->get('http://example.com');

// 可使用数组形式的url
$curl->get(array('http://example.com/search', 'keywords' => 'grass'));

发送post请求

$curl->post('http://example.com/login/', array(
    'username' => 'admin',
    'password' => '123456',
));

发送json数据

$curl->setContentTypeJson()->post('http://example.com/login/', array(
    'username' => 'admin',
    'password' => '123456',
));

文件下载

$curl->download('http://example.com/file.zip', '/path/to/file.zip');

文件上传

$curl->addUploadFile('name', '/path/to/file.zip');
$curl->addUploadFile('img', '/path/to/demo.jpg');
$curl->post('http://example.com/upload.php');

转换结果为json格式

$curl->asJson()->get('http://example.com');

发送其他请求

$curl->put('http://api.example.com/user/', array(
    'name' => 'Grass',
));
$curl->put();
$curl->patch();
$curl->delete();
$curl->options();
$curl->request($url, 'HEAD'); // 自定义请求

获取结果

$curl->curl;                // curl资源句柄
$curl->error_code;          // curl错误码
$curl->error_message;       // curl错误信息
$curl->request_url;         // 请求的url
$curl->request_header;      // 发送的请求头
$curl->request_body;        // 发送的请求体
$curl->request_cookie;      // 发送的cookie
$curl->upload_file;         // 上传的文件
$curl->response;            // 响应体
$curl->response_info;       // curl_getinfo()获取到的响应信息
$curl->response_header;     // 响应头
$curl->response_code;       // HTTP响应的状态码

连贯调用

Curl::instance()->asJson()->get('http://example.com')->response;

多线程请求

$curl1 = new Curl();
$curl2 = new Curl();
Curl::multiExec(array(
    $curl1->multi()->get('http://api.example.com'),
    $curl2->multi()->post('http://api.example.com'),
));
Curl::multiClose();
echo $curl1->response;
echo $curl2->response;

其他可用方法

$curl->setOpt();
$curl->setHeader();
$curl->setCookie();
$curl->setAjax();
$curl->multi();
$curl->asText();
$curl->asJson();
$curl->reset();
$curl->setContentTypeUrlencoded();
$curl->setContentTypeFormData();
$curl->setContentTypeJson();
$curl->setContentTypeXml();
$curl->setDownloadFile();
$curl->setProgressCallback();

批量请求示例

$thread_num = 20;
$urls = array_fill(0, 100, 'https://example.com');

$url_chunks = array_chunk($urls, $thread_num);
$curls = array();
for ($i=0; $i < $thread_num; $i++) { 
    $curls[] = new Curl();
}
foreach ($url_chunks as $chunks) {
    $requests = array();
    foreach ($chunks as $i => $url) {
        $requests[] = $curls[$i]->reset()->multi()->get($url);
    }
    Curl::multiExec($requests);
    foreach ($requests as $i => $curl) {
        echo $curl->response_code, ' ';
    }
}
Curl::multiClose();

执行测试

composer install
phpunit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.