分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

一个PHP高性能、多并发、restful的工具库(基于multi_curl)

发布时间:2023-09-06 01:14责任编辑:赖小花关键词:urlPHP

??This is high performance curl wrapper written in pure PHP. It‘s compatible with PHP 5.4+ and HHVM. Notice that libcurl version must be over 7.36.0, otherwise timeout can not suppert decimal. ??这是一个高性能的PHP封装的HTTP Restful多线程并发请求库,参考借鉴了httpresful 、multirequest等优秀的代码。它与PHP 5.4和hhvm兼容。 注意,libcurl版本必须>=7.36.0,否则超时不支持小数。

  大家好,今天的主角是它: https://github.com/sinacms/MultiHttp ,这是本人写的一个curl工具库,在生产中十分好用,所以拿出来分享给大家,欢迎大家提issue/merge request, 点赞什么的。

<?php
// Include Composer‘s autoload file if not already included.require __DIR__.‘/vendor/autoload.php‘;use MultiHttp\Request;use MultiHttp\Response;//单个请求$responses=array();$responses[] = Request::create()->addQuery(‘wd=good‘)->get(‘http://baidu.com?‘, array( ?????????‘timeout‘ => 3, ?????????‘timeout_ms‘ => 2000, ?????????‘callback‘ => function (Response $response) { ?????????}))->send();$responses[] = Request::create()->get(‘http://qq.com‘, array( ?????????‘callback‘ => function (Response $response) { ?????????????//sth ?????????}))->addOptions(array( ?????????‘method‘ => Request::PATCH, ?????????‘timeout‘ => 3, ?????))->send(); ?????//test post$responses[] = Request::create()->post( ?????‘http://127.0.0.1‘,array(‘data‘=>‘this_is_post_data‘), array( ?????????‘callback‘ => function (Response $response) { ?????????????//sth ?????????}))->send();foreach ($responses as $response) { ?echo $response->request->uri, ‘ takes:‘, $response->duration, ?"\n\t\n\t";}?>//Multi-request 多个请求:<?phpuse MultiHttp\MultiRequest;$mr ?= MultiRequest::create();$rtn = $mr->addOptions( ???array( ???????array( ???????????‘url‘ ???=> ‘http://google.com‘, ???????????‘timeout‘ => 2, ???????????‘method‘ => ‘HEAD‘, ???????????‘data‘ ??=> array( ???????????), ???????????‘callback‘ => function (Response $response) { ???????????????//sth ???????????} ???????), ???)) ???->add(‘GET‘, ‘http://sina.cn‘,array(), array( ???????‘timeout‘ => 3 ???)) ???->import(Request::create()->trace(‘http://sohu.cn‘, array( ???????????‘timeout‘ ?=> 3, ???????????‘callback‘ => function (Response $response) { ???????????????//sth ???????????}))->applyOptions()) ???->send(); ???foreach ($rtn as $response) { ???????echo $response->request->uri, ‘ takes:‘, $response->duration, ‘ ‘, "\n\t\n\t"; ???}?>
options选项有: ??‘url‘ ????????????=> ‘CURLOPT_URL‘, ??‘debug‘ ??????????=> ‘CURLOPT_VERBOSE‘,//for debug verbose ??‘method‘ ?????????=> ‘CURLOPT_CUSTOMREQUEST‘, ??‘data‘ ???????????=> ‘CURLOPT_POSTFIELDS‘, // array or string , file begin with ‘@‘ ??‘ua‘ ?????????????=> ‘CURLOPT_USERAGENT‘, ??‘timeout‘ ????????=> ‘CURLOPT_TIMEOUT‘, // (secs) 0 means indefinitely ??‘connect_timeout‘ => ‘CURLOPT_CONNECTTIMEOUT‘, ??‘referer‘ ????????=> ‘CURLOPT_REFERER‘, ??‘binary‘ ?????????=> ‘CURLOPT_BINARYTRANSFER‘, ??‘port‘ ???????????=> ‘CURLOPT_PORT‘, ??‘header‘ ?????????=> ‘CURLOPT_HEADER‘, // TRUE:include header ??‘headers‘ ????????=> ‘CURLOPT_HTTPHEADER‘, // array ??‘download‘ ???????=> ‘CURLOPT_FILE‘, // writing file stream (using fopen()), default is STDOUT ??‘upload‘ ?????????=> ‘CURLOPT_INFILE‘, // reading file stream ??‘transfer‘ ???????=> ‘CURLOPT_RETURNTRANSFER‘, // TRUE:return string; FALSE:output directly (curl_exec) ??‘follow_location‘ => ‘CURLOPT_FOLLOWLOCATION‘, ??‘timeout_ms‘ ?????=> ‘CURLOPT_TIMEOUT_MS‘, // milliseconds, ?libcurl version > 7.36.0 ,

  

    怎么样,什么人性、直观吧,

  另外,建议大家用最新stable版本, 有很多好用的feature, 比如 expectsJson() 会直接验证response是json,并解析成php array( or hashmap),更多特性请看tests目录的使用。

  欢迎大家使用它。

  我有空会补充些详细说明。

 

一个PHP高性能、多并发、restful的工具库(基于multi_curl)

原文地址:http://www.cnblogs.com/sunsky303/p/7611979.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved