怎样评价libcurl
PHP主干源码就集成了对libcurl的接口扩展php-src/ext/curl.http://php.net/curl
Firefox和Chrome也都提供了对curl的支持,F12开发者工具的网络请求里可以直接把请求复制为cURL命令,主要就是一些头,关键如Cookie信息:curl \u0026#39;https://www.zhihu.com/topic/19552910/newest\u0026#39; \\-H \u0026#39;Host: www.zhihu.com\u0026#39; \\-H \u0026#39;User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0\u0026#39; \\-H \u0026#39;Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\u0026#39; \\-H \u0026#39;Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3\u0026#39; \\-H \u0026#39;Accept-Encoding: gzip, deflate, br\u0026#39; \\-H \u0026#39;Cookie: name1=value1; name2=value2\u0026#39; \\-H \u0026#39;Connection: keep-alive\u0026#39; \\-H \u0026#39;Upgrade-Insecure-Requests: 1\u0026#39;
要实现PHP编程(爬虫,自动化测试),你还可以根据浏览器里得到的命令参数转换为PHP curl代码:http://php.net/curl_setoptCURLOPT_COOKIEFILE:包含 cookie 数据的文件名,cookie 文件的格式可以是 Netscape 格式,或者只是纯 HTTP 头部风格,存入文件。如果文件名是空的,不会加载 cookie,但 cookie 的处理仍旧启用。 CURLOPT_COOKIEJAR:连接结束后,比如,调用 curl_close 后,保存 cookie 信息的文件。
使用PHP curl,单进程也可以并行发起多个请求:接口1: php -S 127.0.0.1:8080 -t /home/eechen/www接口2: php -S 127.0.0.2:8080 -t /home/eechen/www/home/eechen/www/index.php:\u0026lt;?phpheader(\u0026#39;Content-Type: application/json; charset=utf-8\u0026#39;);echo json_encode(array(\u0026#39;SERVER_NAME\u0026#39; =\u0026gt; $_SERVER));//串行访问需要sum(2,1)秒,并行访问需要max(2,1)秒.($_SERVER == \u0026#39;127.0.0.1\u0026#39;) ? sleep(2) : sleep(1);?\u0026gt;并行:/home/eechen/curl.php\u0026lt;?php$url = \u0026#39;http://127.0.0.1:8080\u0026#39;;$url = \u0026#39;http://127.0.0.2:8080\u0026#39;;$mh = curl_multi_init();foreach($url as $k =\u0026gt; $v) {\t$ch = curl_init($v);\tcurl_setopt($ch, CURLOPT_HEADER, 0); //不输出头\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //exec返回结果而不是输出,用于赋值\tcurl_multi_add_handle($mh, $ch); //决定exec输出顺序}$running = null;$starttime = microtime(true);//执行批处理句柄(类似pthreads多线程里的start开始和join同步)do {\t//CURLOPT_RETURNTRANSFER如果为0,这里会直接输出获取到的内容.如果为1,后面可以用curl_multi_getcontent获取内容.\tcurl_multi_exec($mh, $running);\t//阻塞直到cURL批处理连接中有活动连接,不加这个会导致CPU负载超过90%.\tcurl_multi_select($mh);} while ($running \u0026gt; 0);echo microtime(true) - $starttime."\"; //耗时约2秒foreach($ch as $v) {\t$info = curl_getinfo($v);\t$json = curl_multi_getcontent($v);\tcurl_multi_remove_handle($mh, $v);}curl_multi_close($mh);var_export($json); var_export($info);?\u0026gt;输出:2.0015449523926array ( 0 =\u0026gt; \u0026#39;{"SERVER_NAME":"127.0.0.1"}\u0026#39;, 1 =\u0026gt; \u0026#39;{"SERVER_NAME":"127.0.0.2"}\u0026#39;,)array ( 0 =\u0026gt; array ( \u0026#39;url\u0026#39; =\u0026gt; \u0026#39;http://127.0.0.1:8080/\u0026#39;, \u0026#39;content_type\u0026#39; =\u0026gt; \u0026#39;application/json; charset=utf-8\u0026#39;, \u0026#39;http_code\u0026#39; =\u0026gt; 200, \u0026#39;header_size\u0026#39; =\u0026gt; 107, \u0026#39;request_size\u0026#39; =\u0026gt; 53, \u0026#39;filetime\u0026#39; =\u0026gt; -1, \u0026#39;ssl_verify_result\u0026#39; =\u0026gt; 0, \u0026#39;redirect_count\u0026#39; =\u0026gt; 0, \u0026#39;total_time\u0026#39; =\u0026gt; 2.0013990000000002, \u0026#39;namelookup_time\u0026#39; =\u0026gt; 5.3999999999999998E-5, \u0026#39;connect_time\u0026#39; =\u0026gt; 0.00015799999999999999, \u0026#39;pretransfer_time\u0026#39; =\u0026gt; 0.000194, \u0026#39;size_upload\u0026#39; =\u0026gt; 0, \u0026#39;size_download\u0026#39; =\u0026gt; 27, \u0026#39;speed_download\u0026#39; =\u0026gt; 13, \u0026#39;speed_upload\u0026#39; =\u0026gt; 0, \u0026#39;download_content_length\u0026#39; =\u0026gt; -1, \u0026#39;upload_content_length\u0026#39; =\u0026gt; 0, \u0026#39;starttransfer_time\u0026#39; =\u0026gt; 0.00079699999999999997, \u0026#39;redirect_time\u0026#39; =\u0026gt; 0, \u0026#39;certinfo\u0026#39; =\u0026gt; array ( ), \u0026#39;primary_ip\u0026#39; =\u0026gt; \u0026#39;127.0.0.1\u0026#39;, \u0026#39;primary_port\u0026#39; =\u0026gt; 8080, \u0026#39;local_ip\u0026#39; =\u0026gt; \u0026#39;127.0.0.1\u0026#39;, \u0026#39;local_port\u0026#39; =\u0026gt; 57653, \u0026#39;redirect_url\u0026#39; =\u0026gt; \u0026#39;\u0026#39;, ), 1 =\u0026gt; array ( \u0026#39;url\u0026#39; =\u0026gt; \u0026#39;http://127.0.0.2:8080/\u0026#39;, \u0026#39;content_type\u0026#39; =\u0026gt; \u0026#39;application/json; charset=utf-8\u0026#39;, \u0026#39;http_code\u0026#39; =\u0026gt; 200, \u0026#39;header_size\u0026#39; =\u0026gt; 107, \u0026#39;request_size\u0026#39; =\u0026gt; 53, \u0026#39;filetime\u0026#39; =\u0026gt; -1, \u0026#39;ssl_verify_result\u0026#39; =\u0026gt; 0, \u0026#39;redirect_count\u0026#39; =\u0026gt; 0, \u0026#39;total_time\u0026#39; =\u0026gt; 1.0012369999999999, \u0026#39;namelookup_time\u0026#39; =\u0026gt; 1.1E-5, \u0026#39;connect_time\u0026#39; =\u0026gt; 4.6999999999999997E-5, \u0026#39;pretransfer_time\u0026#39; =\u0026gt; 6.3E-5, \u0026#39;size_upload\u0026#39; =\u0026gt; 0, \u0026#39;size_download\u0026#39; =\u0026gt; 27, \u0026#39;speed_download\u0026#39; =\u0026gt; 26, \u0026#39;speed_upload\u0026#39; =\u0026gt; 0, \u0026#39;download_content_length\u0026#39; =\u0026gt; -1, \u0026#39;upload_content_length\u0026#39; =\u0026gt; 0, \u0026#39;starttransfer_time\u0026#39; =\u0026gt; 0.00063699999999999998, \u0026#39;redirect_time\u0026#39; =\u0026gt; 0, \u0026#39;certinfo\u0026#39; =\u0026gt; array ( ), \u0026#39;primary_ip\u0026#39; =\u0026gt; \u0026#39;127.0.0.2\u0026#39;, \u0026#39;primary_port\u0026#39; =\u0026gt; 8080, \u0026#39;local_ip\u0026#39; =\u0026gt; \u0026#39;127.0.0.1\u0026#39;, \u0026#39;local_port\u0026#39; =\u0026gt; 43645, \u0026#39;redirect_url\u0026#39; =\u0026gt; \u0026#39;\u0026#39;, ),)
推荐阅读
- 聪明人养花,这3种“花”怎样也要养一盆,每年能省不少医药费
- 互联网怎样解决“家政服务上门速度慢”的问题
- 怎样看待从1月8号起,QQ钱包开始提现收费
- 银行it人怎样转型
- 汽车|冬天怎样让车内温度快速升高?座椅加热的最佳使用方式二,外循环的作用总结
- 怎样进入通信行业
- 怎样评价扶他柠檬茶的小说《云养汉》的结尾
- 怎样成为一名合格的Python程序员?
- 怎样评价华为、诺基亚、中兴中标中国移动高端路由交换设备扩容集采
- 怎样评价类似前橙会、百老汇、南极圈这样类型的离职帮抱团,对企业的积极意义和消极意义