PHP is more of a pit than to achieve the same purpose, can use super multiple means. For example (File_get_contents and fopen, and today's curl and fsockopen, and of course, sockets) are indeed more difficult to choose for a less experienced program ape. 、
So in very many cases, we need to compare.
Search for irresponsible resources online. Make a prediction. Then weigh it yourself.
The following are excerpts:
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; }
Here are some of the main information about the request header
<?phpError_reporting (e_all ^ e_notice); Ini_set (' Display_errors ',true);//Explain how to use Httpheader. Transfer JSON format data$id=$_post[' id '];$symbol=$_post[' symbol '];$companyName=$_post[' CompanyName '];$data=Array("id"="$id","Symbol"="$symbol","CompanyName"="$companyName");$data _string= Json_encode ($data); Curl_setopt ($ch, Curlopt_postfields,$data _string); Curl_setopt ($ch, Curlopt_httpheader,Array(' Content-type:application/json ',' content-length: '. Strlen$data _string)));?
>
The general process. We're not going to use that complexity:
<?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 cannot be performed on the console, for example PHP xxx.php params_1, which requires server.
Source-php-request-2