Using PHP to emulate the source code of an AJAX request

Source: Internet
Author: User
I have tried it, it is very useful.

/** * Constructs an AJAX request that does not support HTTPS * / functionajax_http_request($url, $data = array(), $type = "Post", $params = array(), $accept = "", $ua = 
     
       "", 
      $referer = 
      "", 
      $file = False)
     {$http= Strtolower (substr ($url,0,7));$type= Strtolower ($type);$url _params= Parse_url ($url);if(!$url _params) {Echo' URL error ';returnfalse; }$hostip= GetHostByName ($url _params[' Host ']);if(!$hostip) {Echo' Unable to access server ';returnfalse; }$fp= Fsockopen ($url _params[' Host '], the,$errno,$errstr, -);if(!$fp) {Echo"$errstr ($errno)
";returnfalse; }$query _string= Http_build_query ($data);if($type==' Post ') {$out=' POST '.$url _params[' path ']."http/1.1\r\n"; }Else{if(Strpos ($url,'?') !=false) {$path=$url.' & '.$query _string; }Else{$path=$url.'?'.$query _string; }$out=' GET '.$path.' http/1.1 '."\ r \ n"; }$out.=' Host: '.$url _params[' Host ']."\ r \ n";$out.="connection:close\r\n";if($type==' Post ') {if($file) {$out.= ("content-type:multipart/form-data\r\n");//Ajax file upload temporarily does not have this feature}Else{$out.= ("content-type:application/x-www-form-urlencoded\r\n"); }$out.= ("Content-length:". strlen ($query _string)."\ r \ n"); }if(isset($ua)) {$out.= (' user-agent: '.$ua."\ r \ n"); }Else{$out.= ("user-agent:mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) applewebkit/537.36 (khtml, like Gecko) chrome/41.0.2272.118 safari/537.36\r\n "); }if(isset($referer)) {$out.= (' referer:http://'.$url _params[' Host '].'/'.$referer."\ r \ n"); }Else{$out.= (' referer:http://'.$url _params[' Host ']."\ r \ n"); }$out.= ("origin:http://".$url _params[' Host ']."\ r \ n");$out.= ("x-requested-with:xmlhttprequest\r\n");$out.= ("Accept:application/json, Text/javascript, */*\r\n");$out.="accept-language:zh-cn,zh;q=0.8,en;q=0.6\r\n\r\n";if($type==' Post ') {//Next is the message body information$out.=$query _string; } fwrite ($fp,$out);$output=""; while(!feof ($fp)) {$output. = Fgets ($fp, -); } fclose ($fp);$result= Explode ("\r\n\r\n",$output);$result= Explode ("\ r \ n",$result[1]); Array_pop ($result); Array_shift ($result);returnImplode ("\ r \ n",$result);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the use of PHP to simulate the source code of AJAX requests, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.