Home > Backend Development > PHP Tutorial > PHP: 6 ways to send GET and POST requests_PHP tutorial

PHP: 6 ways to send GET and POST requests_PHP tutorial

WBOY
Release: 2016-07-12 09:06:39
Original
1022 people have browsed it

PHP: 6 ways to send GET and POST requests

In the i94web blog, I tried two social comment boxes, Changyan and Duosuo, but later I abandoned Changyan because it was not safe.

Whether it is speaking freely or talking more, I need to grab the number of comments on articles remotely and then store them in the local database. For Duosuo, the format of the request is as follows:

<ol class="dp-c"><li class="alt"><span><span class="comment">// 获取评论次数,参数是文章ID</span><span> </span></span></li><li><span><span class="keyword">function</span><span> getCommCount(</span><span class="vars">$postid</span><span>) </span></span></li><li class="alt"><span>{ </span></li><li><span>    <span class="vars">$jsondata</span><span> = </span><span class="func">file_get_contents</span><span>(</span><span class="string">"http://api.duoshuo.com/threads/counts.json?short_name=i94web&threads=$postid"</span><span>); // 设置true返回数组,不设置或者是false则返回对象 </span><span class="vars">$resjson</span><span>= json_decode(</span><span class="vars">$jsondata</span><span>,true); </span><span class="keyword">return</span><span> </span><span class="vars">$resjson</span><span>[</span><span class="string">'response'</span><span>][</span><span class="vars">$postid</span><span>][</span><span class="string">'comments'</span><span>]; </span></span></li><li class="alt"><span>} </span></li></ol>
Copy after login

For remote requests, there are many methods. Today, LZ has collected six types for your reference.

1. Use file_get_contents to get the content in get mode:

<ol class="dp-c"><li class="alt"><span><span><?php </span></span></li><li><span><span class="vars">$url</span><span>=</span><span class="string">'http://www.ido321.com/'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$html</span><span> = </span><span class="func">file_get_contents</span><span>(</span><span class="vars">$url</span><span>); </span></span></li><li><span><span class="func">echo</span><span> </span><span class="vars">$html</span><span>; </span></span></li><li class="alt"><span>?> <br /></span></li></ol>
Copy after login

2. Use fopen to open the url and use get method to obtain

<ol class="dp-c"><li class="alt"><span><span class="vars">$fp</span><span> = </span><span class="func">fopen</span><span>(</span><span class="vars">$url</span><span>, </span><span class="string">'r'</span><span>); </span></span></li><li><span>stream_get_meta_data(<span class="vars">$fp</span><span>); </span></span></li><li class="alt"><span><span class="keyword">while</span><span>(!</span><span class="func">feof</span><span>(</span><span class="vars">$fp</span><span>)) { </span></span></li><li><span><span class="vars">$result</span><span> .= </span><span class="func">fgets</span><span>(</span><span class="vars">$fp</span><span>, 1024); </span></span></li><li class="alt"><span>} </span></li><li><span><span class="func">echo</span><span> </span><span class="string">"url body: $result"</span><span>; </span></span></li><li class="alt"><span>fclose(<span class="vars">$fp</span><span>); </span></span></li></ol>
Copy after login

3. Use file_get_contents to get the content in post mode:

<ol class="dp-j"><li class="alt"><span><span>$data = array (</span><span class="string">'foo'</span><span> => </span><span class="string">'bar'</span><span>); </span></span></li><li><span>$data = http_build_query($data); </span></li><li class="alt"><span>$opts = array ( </span></li><li><span><span class="string">'http'</span><span> => array ( </span></span></li><li class="alt"><span><span class="string">'method'</span><span> => </span><span class="string">'POST'</span><span>, </span></span></li><li><span><span class="string">'header'</span><span>=> </span><span class="string">"Content-type: application/x-www-form-urlencodedrn"</span><span> . </span><span class="string">'Content-Length: '</span><span> . strlen($data) . </span><span class="string">'rn'</span><span>, </span><span class="string">'content'</span><span> => $data ) ); $context = stream_context_create($opts); $html = file_get_contents(</span><span class="string">'http://localhost/e/admin/test.html'</span><span>, </span><span class="keyword">false</span><span>, $context); echo $html; </span></span></li></ol>
Copy after login

4. Use the fsockopen function to open the url and obtain the complete data by get method, including header and body. fsockopen requires the allow_url_fopen option in PHP.ini to be turned on

<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> get_url (</span><span class="vars">$url</span><span>,</span><span class="vars">$cookie</span><span>=false) </span></span></li><li><span>{ </span></li><li class="alt"><span><span class="vars">$url</span><span> = </span><span class="func">parse_url</span><span>(</span><span class="vars">$url</span><span>); </span></span></li><li><span><span class="vars">$query</span><span> = </span><span class="vars">$url</span><span>[path].</span><span class="string">'?'</span><span>.</span><span class="vars">$url</span><span>[query]; </span></span></li><li class="alt"><span><span class="func">echo</span><span> </span><span class="string">'Query:'</span><span>.</span><span class="vars">$query</span><span>; </span></span></li><li><span><span class="vars">$fp</span><span> = </span><span class="func">fsockopen</span><span>( </span><span class="vars">$url</span><span>[host], </span><span class="vars">$url</span><span>[port]?</span><span class="vars">$url</span><span>[port]:80 , </span><span class="vars">$errno</span><span>, </span><span class="vars">$errstr</span><span>, 30); </span></span></li><li class="alt"><span><span class="keyword">if</span><span> (!</span><span class="vars">$fp</span><span>) { </span></span></li><li><span><span class="keyword">return</span><span> false; </span></span></li><li class="alt"><span>} <span class="keyword">else</span><span> { </span></span></li><li><span><span class="vars">$request</span><span> = </span><span class="string">'GET $query HTTP/1.1rn'</span><span>; </span></span></li><li class="alt"><span><span class="vars">$request</span><span> .= </span><span class="string">'Host: $url[host]rn'</span><span>; </span></span></li><li><span><span class="vars">$request</span><span> .= </span><span class="string">'Connection: Closern'</span><span>; </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$cookie</span><span>) </span><span class="vars">$request</span><span>.=</span><span class="string">'Cookie:  $cookien'</span><span>; </span></span></li><li><span><span class="vars">$request</span><span>.=</span><span class="string">'rn'</span><span>; </span></span></li><li class="alt"><span>fwrite(<span class="vars">$fp</span><span>,</span><span class="vars">$request</span><span>); </span></span></li><li><span><span class="keyword">while</span><span>(!@</span><span class="func">feof</span><span>(</span><span class="vars">$fp</span><span>)) { </span></span></li><li class="alt"><span><span class="vars">$result</span><span> .= @</span><span class="func">fgets</span><span>(</span><span class="vars">$fp</span><span>, 1024); </span></span></li><li><span>} </span></li><li class="alt"><span>fclose(<span class="vars">$fp</span><span>); </span></span></li><li><span><span class="keyword">return</span><span> </span><span class="vars">$result</span><span>; </span></span></li><li class="alt"><span>} </span></li><li><span>} </span></li><li class="alt"><span><span class="comment">//获取url的html部分,去掉header</span><span> </span></span></li><li><span><span class="keyword">function</span><span> GetUrlHTML(</span><span class="vars">$url</span><span>,</span><span class="vars">$cookie</span><span>=false) </span></span></li><li class="alt"><span>{ </span></li><li><span><span class="vars">$rowdata</span><span> = get_url(</span><span class="vars">$url</span><span>,</span><span class="vars">$cookie</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$rowdata</span><span>) </span></span></li><li><span>{ </span></li><li class="alt"><span><span class="vars">$body</span><span>= </span><span class="func">stristr</span><span>(</span><span class="vars">$rowdata</span><span>,</span><span class="string">'rnrn'</span><span>); </span></span></li><li><span><span class="vars">$body</span><span>=</span><span class="func">substr</span><span>(</span><span class="vars">$body</span><span>,4,</span><span class="func">strlen</span><span>(</span><span class="vars">$body</span><span>)); </span></span></li><li class="alt"><span><span class="keyword">return</span><span> </span><span class="vars">$body</span><span>; </span></span></li><li><span>} </span></li><li class="alt"><span> </span></li><li><span><span class="keyword">return</span><span> false; </span></span></li><li class="alt"><span>} <br /></span></li></ol>
Copy after login
5. Use the fsockopen function to open the url and obtain the complete data through POST, including header and body
<ol class="dp-c"><li class="alt"><span><span class="keyword">function</span><span> HTTP_Post(</span><span class="vars">$URL</span><span>,</span><span class="vars">$data</span><span>,</span><span class="vars">$cookie</span><span>, </span><span class="vars">$referrer</span><span>=</span><span class="string">''</span><span>) </span></span></li><li><span>{ </span></li><li class="alt"><span><span class="comment">// parsing the given URL</span><span> </span></span></li><li><span><span class="vars">$URL_Info</span><span>=</span><span class="func">parse_url</span><span>(</span><span class="vars">$URL</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// Building referrer</span><span> </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$referrer</span><span>==</span><span class="string">''</span><span>) </span><span class="comment">// if not given use this script as referrer</span><span> </span></span></li><li><span><span class="vars">$referrer</span><span>=</span><span class="string">'111'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span><span class="comment">// making string from $data</span><span> </span></span></li><li class="alt"><span><span class="keyword">foreach</span><span>(</span><span class="vars">$data</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$key</span><span>=></span><span class="vars">$value</span><span>) </span></span></li><li><span><span class="vars">$values</span><span>[]=</span><span class="string">'$key='</span><span>.urlencode(</span><span class="vars">$value</span><span>); </span></span></li><li class="alt"><span><span class="vars">$data_string</span><span>=implode(</span><span class="string">'&'</span><span>,</span><span class="vars">$values</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// Find out which port is needed &ndash; if not given use standard (=80)</span><span> </span></span></li><li><span><span class="keyword">if</span><span>(!isset(</span><span class="vars">$URL_Info</span><span>[</span><span class="string">'port'</span><span>])) </span></span></li><li class="alt"><span><span class="vars">$URL_Info</span><span>[</span><span class="string">'port'</span><span>]=80; </span></span></li><li><span> </span></li><li class="alt"><span><span class="comment">// building POST-request:</span><span> </span></span></li><li><span><span class="vars">$request</span><span>.=</span><span class="string">"POST "</span><span>.</span><span class="vars">$URL_Info</span><span>[</span><span class="string">'path'</span><span>].</span><span class="string">" HTTP/1.1n"</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">"Host: "</span><span>.</span><span class="vars">$URL_Info</span><span>[</span><span class="string">'host'</span><span>].</span><span class="string">"n"</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">"Referer: $referern"</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">"Content-type: application/x-www-form-urlencodedn"</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">'Content-length: '</span><span>.</span><span class="func">strlen</span><span>(</span><span class="vars">$data_string</span><span>).</span><span class="string">"n"</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">'Connection: closen'</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">'Cookie: $cookien'</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="string">'n'</span><span>; </span><span class="vars">$request</span><span>.=</span><span class="vars">$data_string</span><span>.</span><span class="string">'n'</span><span>; </span><span class="vars">$fp</span><span> = </span><span class="func">fsockopen</span><span>(</span><span class="vars">$URL_Info</span><span>[</span><span class="string">'host'</span><span>],</span><span class="vars">$URL_Info</span><span>[</span><span class="string">'port'</span><span>]); </span><span class="func">fputs</span><span>(</span><span class="vars">$fp</span><span>, </span><span class="vars">$request</span><span>); </span><span class="keyword">while</span><span>(!</span><span class="func">feof</span><span>(</span><span class="vars">$fp</span><span>)) { </span><span class="vars">$result</span><span> .= </span><span class="func">fgets</span><span>(</span><span class="vars">$fp</span><span>, 1024); } fclose(</span><span class="vars">$fp</span><span>); </span><span class="keyword">return</span><span> </span><span class="vars">$result</span><span>; </span></span></li><li class="alt"><span>} </span></li></ol>
Copy after login
6. Use the curl library. Before using the curl library, you may need to check whether the curl extension has been turned on in php.ini
<ol class="dp-c"><li class="alt"><span><span class="vars">$ch</span><span> = curl_init(); </span></span></li><li><span><span class="vars">$timeout</span><span> = 5; </span></span></li><li class="alt"><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_URL, &lsquo;http:</span><span class="comment">//www.ido321.com/');</span><span> </span></span></li><li><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_RETURNTRANSFER, 1); </span></span></li><li class="alt"><span>curl_setopt (<span class="vars">$ch</span><span>, CURLOPT_CONNECTTIMEOUT, </span><span class="vars">$timeout</span><span>); </span></span></li><li><span><span class="vars">$file_contents</span><span> = curl_exec(</span><span class="vars">$ch</span><span>); </span></span></li><li class="alt"><span>curl_close(<span class="vars">$ch</span><span>); </span></span></li><li><span><span class="func">echo</span><span> </span><span class="vars">$file_contents</span><span>; </span></span></li></ol>
Copy after login



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1064458.htmlTechArticlePHP: 6 ways to send GET and POST requests In the i94web blog, I tried Changyan and Duosuo The two social comment boxes later abandoned free speech and were unsafe. Whether speaking freely or talking more...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template