PHP常见的三种HTTP请求

socket编程来直接给接口发送数据来模拟post的操作

建立两个文件post.php,getpost.php
post.php内容如下:
<?php
 $flag = 0;
 $post = '';
 $errno = '';
 $errstr = '';
 //要post的数据
$argv = array(
    'var1'=>'abc',
    'var2'=>'how are you , my friend??'
);
//构造要post的字符串
foreach ($argv as $key=>$value) {
    if ($flag!=0) {
        $post .= "&";
        $flag = 1;
    }
    $post.= $key."="; $post.= urlencode($value);
    $flag = 1;
    }
    $length = strlen($post);
     //创建socket连接
    $fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
    //构造post请求的头
    $header  = "POST /flandy/getpost.php HTTP/1.1\r\n";//一般有post, get这两种
    $header .= "Host:127.0.0.1\r\n";
    $header .= "Referer:/flandy/post.php\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: ".$length."\r\n";
    $header .= "Connection: Close\r\n\r\n";
    //添加post的字符串
    $header .= $post."\r\n";
    

    //发送post的数据
    fputs($fp,$header);
    $inheader = 1;
    while (!feof($fp)) {
        $line = fgets($fp,1024); //去除请求包的头只显示页面的返回数据
        if ($inheader && ($line == "\n" || $line == "\r\n")) {
             $inheader = 0;
        }
        if ($inheader == 0) {
          echo $line;
        }
    }

fclose($fp);
?>

getpost.php的内容如下
<?php
echo "this is the data posted";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
结果输出:
this is the data posted

Array(
[var1] => abc
[var2] => how are you , my friend??
)

curl扩展或HttpClient.class.php类,这两个非常类似,下面简单的列出curl的实现代码

<?php
$psecode = ’NDE005’;
$website = ’www.baidu.com’;
$amt = 1;
$pwd = 123456;
$ch = curl_init();
$curl_url = "http://localhost:81/flandy/getpost2.php?web=" . $website .
"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量
$curl_result = curl_exec($ch);
$result = explode(',', $curl_result);
curl_close($ch);
print_r($result);
?>

<?php
echo "returndata<br>";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
结果输出:
Array (
  [0] => returndata
  Array(
    [web] => ’wwwbaiducom’
    [pwd] => 123456
    [action] => check
    [pseid] => ’NDE005’
    [amt] => 1
 )
)

这个要借助第三方类库HttpClient

可以到这里下载: http://scripts.incutio.com/httpclient/
<?php
require_once 'HttpClient.class.php’;
$params = array(’web’ => ’www.baidu.com’,
’pwd’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’http://localhost:81/flandy/getpost3.php’, $params);
$result = explode(’,’, $pageContents);
print_r($result);
?>

补充:
HTTP请求常用方案有以下(友情提示:排列顺序只为所想起时的先后顺序并再无特别含义)
1.HttpClient 
Version 0.9, Simon Willison April 6th 2003
http://scripts.incutio.com/httpclient/HttpClient.class.php

2.snoopy
Snoopy Snoopy 1.2.3 November 7, 2005 
http://snoopy.sourceforge.net/

3.pear::http_client
1.1.0 (stable) was released on 2006-06-03
http://pear.php.net/package/HTTP_Client

4.curl or php_curl
5.wget
6.php_socket


3个算是比较完整的类,所以后面的暂时不考虑了。
库的选择一般原则是找用的人多,更新持久的,因此 pear::http_client 一马当先,但这个必须是统筹在PEAR之下,因为要用到一些PEAR的辅助类,不是很适合单独使用,请回去等录用通知吧。这回合Snoopy 领先一步,但粗略一看核心文件Snoopy.class.php 体重38KB,再看 HttpClient 感觉是相当苗条了,核心文件 HttpClient.class.php 占地12KB,这回合 HttpClient 也得一分,不过最后更新日期让人看得心寒。

人气测试(pear::http_client友情出场):
1.Google Trends 
结果:放弃。
因为 Snoopy 在某个世界实在太有名气了,而且"http client" 关键字也太含糊。

2.Google Code Search
规则:php + 包含类名的一行并用双引号括起来
HttpClient 100
http://www.google.com/codesearch ... class+HttpClient"

Snoopy 100
http://www.google.com/codesearch ... +"class+Snoopy"

pear::http_client 12 (还是请继续回去等通知吧)
http://www.google.com/codesearch ... "&btnG=Search

核心PK
一般来说,php HTTP CLIENT都是通过PHP_CURL或者PHP_SOCKET来实现的,


案例1解析:

PHP构造http头发送请求

http头部的格式和参数说明

 php发送http请求的最关键点在于构造一个符合http协议的头部,http请求的信息一般以下几个方面构成

(1)http method:一般有post, get这两种

(2)request url:http所请求的资源

(3)http version:用哪一个http版本协议,常见的有HTTP/1.1 和HTTP/1.0

(4)Host:请求的主机名称

(5)User-Agent:用户代理类别,一般是浏览器名称,如firefox的User-Agent:Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (luci    d) Firefox/3.6.15一般从User-Agent中可以知道用户用的是什么类型的浏览器和操作系统,以级他们的版本

(6)Accept:为客户端可以接受的媒体类型,常见的有text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

(7)Accept-Language:可以接受的语言类型,如en-gb,en;q=0.5

(8)Accept-Encoding:可以接受的编码类型,如gzip,deflate,可以接受压缩

(9)Accept-Charset:可以接受的字体编码,如ISO-8859-1,utf-8;q=0.7,*;q=0.7

(10)Content-Type:内容传送的类型,如application/x-www-form-urlencoded,如普通的form提交方式

(11)Cookie:要传送到服务器上的cookie数据,如AJSTAT_ok_times=2; SESS_ID=c8c293c948c233a9a89aa11fb08c61af5f29c7ef; AJSTAT_ok_pages=2

(12)Content-Length:除了头部,传送内容的长度。

(13)Referer:也就是先前访问的页面.

Content-Type常见有如下类型,因为content-Type和数据传送的格式有很大的关系,所以这里详细说明数据提交的类型

关于application/x-www-form-urlencoded等字符编码的解释说明

在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。

下边是说明:

 application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。

 multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。

 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。

 

  form的enctype属性为编码方式,

 常用有两种:application/x-www-form-urlencoded和multipart/form-data,

 默认为application/x-www-form-urlencoded。

 当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),

 然后把这个字串append到url后面,用?分割,加载这个新的url。

 当action为post时候,浏览器把form数据封装到http body中,然后发送到server。

 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了。

 但是如果有type=file的话,就要用到multipart/form-data了。

 浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),

  Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

php构造http请求
<?
if (empty ($_REQUEST['email'])) {
$fp = fsockopen("www1.header.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)
/n";
} else {
$out = "POST /index.php HTTP/1.1/r/n";
$out .= "Host: www1.header.com/r/n";
$out .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.15) Gecko/20110303 Ubuntu/10.04 (lucid) Firefox/3.6.15/r/n";
$out .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8/r/n";
$out .= "Accept-Language: en-gb,en;q=0.5/r/n";
$out .= "Accept-Encoding: gzip,deflate/r/n";
$out .= "Content-Type: application/x-www-form-urlencoded/r/n";
$out .= "Content-Length: 80/r/n";
$out .= "Connection: Close/r/n/r/n";
$out .= "email=youname%40gmail.com&password=youpasswd&act=login&redirectURL=&loginsubmit=/r/n/r/n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
} else {
print_r($_REQUEST);
}
?>

用php发送http请求的主要分两部

(1)构造一个http头部的串.

(2)用fsockopen打开socket连接。

(3)再用fwrite把构造好的数据传送到请求主机


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值