糯米文學吧

位置:首頁 > 計算機 > php語言

PHP的GET和POST請求發送方法

php語言2.86W

在i94web博客中,我試過了暢言和多説兩種社會化評論框,後來還是拋棄了暢言,不安全。無論是暢言還是多説,我都需要從遠程抓取文章的.評論數,然後存入本地數據庫。對於多説,請求的格式如下,就跟隨小編去了解下吧,想了解更多相關信息請 持續關注我們應屆畢業生考試網!

PHP的GET和POST請求發送方法

  // 獲取評論次數,參數是文章ID

function getCommCount($postid)

{

$jsondata = file_get_contents("_name=i94web&threads=$postid"); // 設置true返回數組,不設置或者是false則返回對象 $resjson= json_decode($jsondata,true); return $resjson['response'][$postid]['comments'];

}

對於遠程請求,有很多種方法。今天,LZ就蒐羅了六種,供大家參考。

  1、用file_get_contents 以get方式獲取內容:

<?php

$url='';

$html = file_get_contents($url);

echo $html;

?>

  2、用fopen打開url,用get方式獲取

$fp = fopen($url, 'r');

stream_get_meta_data($fp);

while(!feof($fp)) {

$result .= fgets($fp, 1024);

}

echo "url body: $result";

fclose($fp);

  3、用file_get_contents 以post方式獲取內容:

$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/', false, $context); echo $html;

  4、用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body,fsockopen需要 中 allow_url_fopen 選項開啟

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 {

標籤:PHP Post 發送