Heim >Backend-Entwicklung >PHP-Tutorial >PHP:6种GET和POST请求发送方法_PHP教程
在i94web博客中,我试过了畅言和多说两种社会化评论框,后来还是抛弃了畅言,不安全。
无论是畅言还是多说,我都需要从远程抓取文章的评论数,然后存入本地数据库。对于多说,请求的格式如下:
<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>
对于远程请求,有很多种方法。今天,LZ就搜罗了六种,供大家参考。
1、用file_get_contents 以get方式获取内容:
<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>
2、用fopen打开url,用get方式获取
<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>
3、用file_get_contents 以post方式获取内容:
<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>
4、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启
<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>5、用fsockopen函数打开url,以POST方式获取完整的数据,包括header和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 – 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>6、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
<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, ‘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>