在i94web部落格中,我試過了暢言和多說兩種社會化評論框,後來還是拋棄了暢言,不安全。無論是暢言還是多說,我都需要從遠端抓取文章的.評論數,然後存入本地資料庫。對於多說,請求的格式如下,就跟隨小編去了解下吧,想了解更多相關資訊請 持續關注我們應屆畢業生考試網!
// 獲取評論次數,引數是文章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 {