The place where PHP compares pits is to achieve the same goal, and can use super multiple means. For example (File_get_contents and fopen, and now the curl and fsockopen, and of course, sockets) are really hard to choose for a less experienced programmer. 、
So in many cases, we need to compare. Search for irresponsible resources online and make a prediction. Then weigh it yourself.
Here's a excerpt:
function curlrequest($url, $isPost = False, $param = array() , $cookie = null, $header = null, $referer = "", $timeout = 6 0) { $ch= Curl_init (); curl_setopt ($ch, Curlopt_url,$url);if(Is_array ($header) &&!Empty($header)) {//Set Header $set _head=Array();foreach($header as $k=$v) {$set _head[] ="$k: $v"; } curl_setopt ($ch, Curlopt_httpheader,$set _head); }Else{curl_setopt ($ch, Curlopt_header,0); }if($isPost===true) {curl_setopt ($ch, Curlopt_post,$isPost); curl_setopt ($ch, Curlopt_postfields,$param); }if(!Empty($cookie)) {if(Is_array ($cookie)) {$cookies= Implode ('; ',$cookie); }Else{$cookies=$cookie; } curl_setopt ($ch, Curlopt_cookie,$cookies); }if(!Empty($referer) {curl_setopt ($ch, Curlopt_referer,$referer); }if($timeout>0) {curl_setopt ($ch, Curlopt_timeout,$timeout); } curl_setopt ($ch, Curlopt_returntransfer,true);$ret= Curl_exec ($ch);if(Curl_errno ($ch)) {$ret="'; } curl_close ($ch);return $ret; }
The general process, we do not use so complex:
<?php$startTime= Microtime (true);$url="http://api.k780.com:88/?app=weather.today&weaid=1&appkey=10003&sign= B59bc3ef6191eb9f747dd4e83c99f2a4&format=xml "; for($i=0;$i<2;$i++){$handle= Curl_init ($url); //$defaultHeader= Array (' accept-encoding:gzip, default '); curl_setopt ($handle, Curlopt_returntransfer,1); curl_setopt ($handle, Curlopt_httpheader,$defaultHeader); curl_setopt ($handle, Curlopt_connecttimeout,Ten); curl_setopt ($handle, Curlopt_timeout,Ten);$xml= Curl_exec($handle);if($i%Ten==0){Echo "."; } curl_close ($handle); }$endTime= Microtime (true);$total=$endTime-$startTime;Echo $total;EchoCurl_error ($handle);? >
It is necessary to note that Curl is not operational in the console, such as PHP xxx.php Params_1, which requires a server.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Source-php-request-2