Source-php-request-2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.