Php:6 get and Post request sending method _php Tutorial

Source: Internet
Author: User
Tags vars

Php:6 kinds of Get and post request sending methods


In the I94web blog, I tried to Chang and say two social comment boxes, and later abandoned the smooth speech, unsafe.

I need to crawl the number of comments from the article remotely and then deposit it into the local database, whether it's a word or a lot. For more said, the requested format is as follows:

 
 
    1. //get comment count, argument is article ID
    2. function getcommcount ( $postid )
    3. {
    4. $jsondata span> = file_get_contents ( ); Sets true to return an array, not set or false to return an object $resjson = Json_decode ( $jsondata , True); return $resjson [ [ $postid [ ' comments ' ];
    5. }

There are a number of ways to do this for remote requests. Today, the LZ has collected six kinds, for everyone's reference.

1, use file_get_contents to get the content:

 
  
  
  1. $url = ' http://www.ido321.com/'
  2. $htmlfile_get_contents($url
  3. echo$html

2, open the URL with fopen, get the way to obtain

 
  
  
  1. $fp = fopen ( $url , ' R ' );
  2. stream_get_meta_data ( $fp );
  3. while (! feof ( $fp )) {
  4. $result . = fgets ( $fp , 1024x768);
  5. }
  6. echo " URL body: $result " ;
  7. fclose ( $fp );

3. Use file_get_contents to post to get content:

 
 
  1. $data = Array (' foo ' = ' bar');
  2. $data = Http_build_query ($data);
  3. $opts = Array (
  4. ' http ' = = Array (
  5. ' method ' = ' POST ' ,
  6. ' Header ' = "Content-type:application/x-www-form-urlencodedrn" . ' content-length: ' . strlen ($data). ' RN ' , ' content ' = $data)); $context = Stream_context_create ($opts); $html = file_get_contents (' http://localhost/e/admin/test.html ', false , $context); Echo $html;

4. Open the URL with the Fsockopen function, get the complete data in Get mode, including header and Body,fsockopen need php.ini allow_url_fopen option to open

 
 
  1. function Get_url ($url,$cookie=false)
  2. {
  3. $url = Parse_url ($url);
  4. $query = $url [path]. '?' . $url [query];
  5. Echo ' Query: '. $query ;
  6. $fp = Fsockopen ( $url[host], $url[port]? $url [Port]:80, $errno , $errstr , 30);
  7. if (! $fp ) {
  8. return false;
  9. } Else {
  10. $request = ' GET $query http/1.1rn ' ;
  11. $request .= ' Host: $url [host]rn ' ;
  12. $request .= ' Connection:closern ' ;
  13. if ($cookie) $request .= ' Cookie: $cookien ' ;
  14. $request .= ' RN ' ;
  15. Fwrite ($fp,$request);
  16. while (!@feof($fp)) {
  17. $result .= @ fgets ($fp, 1024x768);
  18. }
  19. Fclose ($fp);
  20. return $result;
  21. }
  22. }
  23. //Get the HTML part of the URL and remove the header
  24. function geturlhtml ($url,$cookie=false)
  25. {
  26. $rowdata = Get_url ($url,$cookie);
  27. if ($rowdata)
  28. {
  29. $body = Stristr ($rowdata,' rnrn ');
  30. $body = substr ($body, 4,strlen($body));
  31. return $body;
  32. }
  33. return false;
  34. }
5. Open the URL with the Fsockopen function to get the complete data by post, including header and body
 
 
  1. function http_post ($URL,$data,$cookie, $referrer ='')
  2. {
  3. //parsing the given URL
  4. $URL _info = Parse_url ($URL);
  5. //Building referrer
  6. if ($referrer= =") //If not given with this script as referrer
  7. $referrer = ' 111 ' ;
  8. //making string from $data
  9. foreach ( $data as $key=$value )
  10. $values []= ' $key = ' . UrlEncode ($value);
  11. $data _string =implode (' & ',$values);
  12. //Find out which port was needed–if not given use standard (=80)
  13. if (!isset ($URL _info[' Port '] )
  14. $URL _info [' Port ']=80;
  15. //Building post-request:
  16. $request.="POST".$URL _info[' path ']."http/1.1n"; $request.="Host:".$URL _info[' Host ']."n"; $request.="Referer: $referern"; $request.="Content-type:application/x-www-form-urlencodedn"; $request.=' content-length: '.strlen($data _string)."n"; $request.=' Connection:closen '; $request.=' Cookie: $cookien '; $request.=' n '; $request.=$data _string.' n '; $fp = Fsockopen($URL _info[' Host '],$URL _info[' Port ']); fputs($fp, $request); while(!feof($fp)) { $result .= fgets($fp, 1024); } fclose ($fp); return $result;
  17. }
6. Using the Curl Library, before using the Curl Library, you may need to check to see if the php.ini has already turned on the Curl extension
 
 
    1. $ch = Curl_init ();
    2. $timeout = 5;
    3. curl_setopt ( $ch , Curlopt_url, ' http: //www.ido321.com/');
    4. curl_setopt ( $ch , Curlopt_returntransfer, 1); /span>
    5. curl_setopt ( $ch , Curlopt_connecttimeout , $timeout );
    6. $file _contents = curl_exec ( $ch );
    7. curl_close ( $ch );
    8. echo $file _contents ;

http://www.bkjia.com/PHPjc/1064458.html www.bkjia.com true http://www.bkjia.com/PHPjc/1064458.html techarticle Php:6 get and POST request sending methods in the I94web blog, I tried to talk about the two social comment boxes, and later abandoned the smooth speech, unsafe. Whether it is a smooth speech or more ...

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