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:
- //get comment count, argument is article ID
- function getcommcount ( $postid )
- {
- $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 ' ];
- }
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:
-
- $url = ' http://www.ido321.com/'
- $htmlfile_get_contents($url
- echo$html
2, open the URL with fopen, get the way to obtain
- $fp = fopen ( $url , ' R ' );
- stream_get_meta_data ( $fp );
- while (! feof ( $fp )) {
- $result . = fgets ( $fp , 1024x768);
- }
- echo " URL body: $result " ;
- fclose ( $fp );
3. Use file_get_contents to post to get content:
- $data = Array (' foo ' = ' bar');
- $data = Http_build_query ($data);
- $opts = Array (
- ' http ' = = Array (
- ' method ' = ' POST ' ,
- ' 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
- function Get_url ($url,$cookie=false)
- {
- $url = Parse_url ($url);
- $query = $url [path]. '?' . $url [query];
- Echo ' Query: '. $query ;
- $fp = Fsockopen ( $url[host], $url[port]? $url [Port]:80, $errno , $errstr , 30);
- if (! $fp ) {
- return false;
- } Else {
- $request = ' GET $query http/1.1rn ' ;
- $request .= ' Host: $url [host]rn ' ;
- $request .= ' Connection:closern ' ;
- if ($cookie) $request .= ' Cookie: $cookien ' ;
- $request .= ' RN ' ;
- Fwrite ($fp,$request);
- while (!@feof($fp)) {
- $result .= @ fgets ($fp, 1024x768);
- }
- Fclose ($fp);
- return $result;
- }
- }
- //Get the HTML part of the URL and remove the header
- function geturlhtml ($url,$cookie=false)
- {
- $rowdata = Get_url ($url,$cookie);
- if ($rowdata)
- {
- $body = Stristr ($rowdata,' rnrn ');
- $body = substr ($body, 4,strlen($body));
- return $body;
- }
- return false;
- }
5. Open the URL with the Fsockopen function to get the complete data by post, including header and body
- function http_post ($URL,$data,$cookie, $referrer ='')
- {
- //parsing the given URL
- $URL _info = Parse_url ($URL);
- //Building referrer
- if ($referrer= =") //If not given with this script as referrer
- $referrer = ' 111 ' ;
- //making string from $data
- foreach ( $data as $key=$value )
- $values []= ' $key = ' . UrlEncode ($value);
- $data _string =implode (' & ',$values);
- //Find out which port was needed–if not given use standard (=80)
- if (!isset ($URL _info[' Port '] )
- $URL _info [' Port ']=80;
- //Building post-request:
- $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;
- }
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
- $ch = Curl_init ();
- $timeout = 5;
- curl_setopt ( $ch , Curlopt_url, ' http: //www.ido321.com/');
- curl_setopt ( $ch , Curlopt_returntransfer, 1); /span>
- curl_setopt ( $ch , Curlopt_connecttimeout , $timeout );
- $file _contents = curl_exec ( $ch );
- curl_close ( $ch );
- 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 ...