PHP的三种HTTP请求,php模拟post 提交
方法一:利用php的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";
$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
[var1] => abc
[var2] => how are you , my friend??
)
方法二:
使用PHP的curl扩展或HttpClient.class.php类,这两个非常类似,下面简单的列出curl的实现代码。
两个文件post2.php和getpost2.php
post2.php的内容如下:
<?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);
?>
getpost2.php的内容如下:
<?php
echo "returndata<br>";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
结果输出:
Array (
[0] => returndata Array(
[web] => ’wwwbaiducom’
[pwd] => 123456
[action] => check
[pseid] => ’NDE005’
[amt] =>
)
) 方法三:这个要借助第三方类库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 3.pear::http_client 4.curl or php_curl 前3个算是比较完整的类,所以后面的暂时不考虑了。 人气测试(pear::http_client友情出场): 2.Google Code Search Snoopy 100 pear::http_client 12 (还是请继续回去等通知吧) 核心PK: 功能PK: |
各大搜索引擎网站免费登录入口
一:百度网站登录入口
网址:http://www.baidu.com/search/url_submit.html
说明:
一个免费登录网站只需提交一页(首页),百度搜索引擎会自动收录网页。
符合相关标准您提交的网址,会在1个月内按百度搜索引擎收录标准被处理。
百度不保证一定能收录您提交的网站。
二:google网站登陆入口,将网址添加到Google
网址:http://www.google.com/addurl/?hl=zh-CN&continue=/addurl
说明:
请输入完整的网址,包括 http:// 的前缀。例如: http://www.google.cn/ 。您还可以添加评论或关键字,对您网页的内容进行描述。这些内容仅供我们参考,并不会影响 Google 如何为您的网页编排索引或如何使用您的网页。
请注意:您只需提供来自托管服务商的顶层网页即可,不必提交各个单独的网页。我们的抓取工具 Googlebot 能够找到其他网页。Google 会定期更新它的索引,因此您无需提交更新后的或已过期的链接。无效的链接会在我们下次抓取时(即更新整个索引时)淡出我们的索引。
网址:http://www.soso.com/help/usb/urlsubmit.shtml
注:若您所提交的站点地址符合规范,SOSO会尽快完成对您提交的站点审核和抓取。每个网站只需提交一页(首页),搜搜会自动收录网页。如果您提交的网址符合相关标准,搜搜会在1个月内按收录标准处理您的网站。收录标准见帮助文档。
四:雅虎网站登陆入口
网址:http://search.help.cn.yahoo.com/h4_4.html
*请注意:
1.本服务由搜索引擎自动抓取网站信息,不保证会收录所有的网站,也不提供网站描述语编辑及相关修改服务。
2.网站被收录的速度依搜索引擎更新速度而定,可能需要花费数周至数月时间;
3.网站无法被抓取的原因可能是:网站无法连结、网站设定了拒绝被抓取的指令等因素,您可参考如何有效让搜索引擎抓取您的网站等相关说明。
4.本服务为搜索引擎自动抓取网站信息,故无法查询提交的进度。
五:必应网站登陆入口
网址:http://cn.bing.com/webmaster/SubmitSitePage.aspx?mkt=zh-CN
六:有道网站登陆入口
网址:http://tellbot.youdao.com/report
网址:http://www.sogou.com/feedback/urlfeedback.php
八:Alexa网站登录入口
网址:http://www.alexa.com/help/webmasters
九:中国搜索网站登录入口
网址:http://ads.zhongsou.com/register/page.jsp
十:Dmoz网站登录入口
网址:http://www.dmoz.com/World/Chinese_Simplified
十一:Coodir网站目录登录入口
网址:http://www.coodir.com/accounts/addsite.asp
十二:Onebigdirectory.com 搜索引擎批量提交
网址:http://www.onebigdirectory.com/cgi-bin/dir/addurl.cgi
十三:Freewebsubmission.com 搜索引擎批量提交
网址:http://www.freewebsubmission.com/
十四:搜索引擎收录查询
网址:http://indexed.webmasterhome.cn/