i94web博客中,我试过了畅言和多说两种社会化评论框,后来还是抛弃了畅言,不安全。

无论是畅言还是多说,我都需要从远程抓取文章的评论数,然后存入本地数据库。对于多说,请求的格式如下:

// 获取评论次数,参数是文章ID
function getCommCount($postid)
{
$jsondata = file_get_contents("http://api.duoshuo.com/threads/counts.json?short_name=i94web&threads=$postid");
// 设置true返回数组,不设置或者是false则返回对象
$resjson= json_decode($jsondata,true);
return $resjson['response'][$postid]['comments'];
}

对于远程请求,有很多种方法。今天,LZ就搜罗了六种,供大家参考。

1、用file_get_contents 以get方式获取内容:

<?php
$url='http://www.ido321.com/';
$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/test.html', false, $context); echo $html;

    4、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 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 {
$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, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉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、用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

function HTTP_Post($URL,$data,$cookie, $referrer='')
{ // parsing the given URL
$URL_Info=parse_url($URL); // Building referrer
if($referrer=='') // if not given use 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 is 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、使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, ‘http://www.ido321.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch); echo $file_contents;

PHP:6种GET和POST请求发送方法的更多相关文章

  1. 6种GET和POST请求发送方法

    我试过了畅言和多说两种社会化评论框,后来还是抛弃了畅言,不安全. 无论是畅言还是多说,我都需要从远程抓取文章的评论数,然后存入本地数据库.对于多说,请求的格式如下: // 获取评论次数,参数是文章ID ...

  2. 跨域访问 - 跨域请求 同源策略概念对跨域请求的影响 及几种解决跨域请求的方法如 jsonp

    为什么会设置同源策略 > 适用于浏览器的一种资源访问策略 > 同源策略(Same origin policy)是一种约定,它是浏览器最核 心也最 基本的安全功能,如果缺少了同源策略,则浏览 ...

  3. chrome 浏览器的预提取资源机制导致的一个请求发送两次的问题以及ClientAbortException异常

    调查一个 pdf 打印报错: ExceptionConverter: org.apache.catalina.connector.ClientAbortException: java.net.Sock ...

  4. 用Python做大批量请求发送

    大批量请求发送需要考虑的几个因素: 1. 服务器承载能力(网络带宽/硬件配置); 2. 客户端IO情况, 客户端带宽, 硬件配置; 方案: 1. 方案都是相对的; 2. 因为这里我的情况是客户机只有一 ...

  5. 发送http请求的方法

    在http/1.1 协议中,定义了8种发送http请求的方法 get post options head put delete trace connect patch. 根据http协议的设计初衷,不 ...

  6. 一个http请求发送到后端的详细过程

    我们来看当我们在浏览器输入http://www.mycompany.com:8080/mydir/index.html,幕后所发生的一切. 首先http是一个应用层的协议,在这个层的协议,只是一种通讯 ...

  7. 【JAVA】通过HttpClient发送HTTP请求的方法

    HttpClient介绍 HttpClient 不是一个浏览器.它是一个客户端的 HTTP 通信实现库.HttpClient的目标是发 送和接收HTTP 报文.HttpClient不会去缓存内容,执行 ...

  8. Web爬去的C#请求发送

    public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...

  9. ajax请求发送和页面跳转的冲突

    http://blog.csdn.net/allenliu6/article/details/77341538?locationNum=7&fps=1 在A页面中,有一个click事件,点击时 ...

随机推荐

  1. c/c++优秀博文

    C进阶指南(1):整型溢出和类型提升.内存申请和管理 http://blog.jobbole.com/72830/ 软件开发中应避免的10个问题

  2. HTML5入门1---Canvas画布

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. Dropbox 有哪些鲜为人知的使用技巧?

    作者:Feeng链接:http://www.zhihu.com/question/20104959/answer/13991578来源:知乎著作权归作者所有,转载请联系作者获得授权. 原文:The B ...

  4. 在Eclipse下debug 出现Source not found for ...

    在Eclipse下debug 出现Source not found for ... 在Eclipse下调试Servlet出现了Source not found for XxxAction.execut ...

  5. highcharts 结合phantomjs纯后台生成图片系列二之php

    上篇文章中介绍了phantomjs的使用场景,方法.本篇文章详细介绍使用php,highcharts 结合phantomjs纯后台生成图片. 一.准备: 下载phantomjs解析插件,从 highc ...

  6. [置顶] Android系统五大布局详解Layout

    我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...

  7. GridLayoutManager

    GridLayoutManager Class Overview A RecyclerView.LayoutManager implementations that lays out items in ...

  8. Myeclipse多行注释快捷键及其他快捷键

    当时我看到struts2讲解视频的时候,讲解员居然能一下子注释掉好几行代码,而且注释的很整齐,然我大吃一惊,上网搜了下Myeclipse的快捷键还真多 选择你要注释的那一行或多行代码,按Ctrl+/即 ...

  9. [HDOJ3974]Assign the task(建树胡搞)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 出现在窝bin的线段树专题里…第一时间想的是记录入度找出根节点,然后标记深度转换到线段树中.但是 ...

  10. CY7C68013A的一点总结

    一. 值得参考的资料:FX2 TechRefManual.USB应用开发宝典. LabVIEW-USB通信简单教程(用于参考生成labview驱动程序).USB设备请求和描述符整理(仅用于理解描述符的 ...