Home > php教程 > php手册 > php curl模拟post请求提交数据

php curl模拟post请求提交数据

WBOY
Release: 2016-06-21 08:47:58
Original
1240 people have browsed it

 最近在做校园图书馆图书信息的采集程序,既然是图书馆图书的采集,肯定有提交搜索的页面,无非是post提交,让我想到了curl模拟提交,首先通过firebug进行抓包查询下post提交后的格式如下:

 

txtWxlx=CN&hidWxlx=spanCNLx&txtPY=HZ&txtTm=%D2%F4%C0%D6&txtLx=%25&txtSearchType=1&nMaxCount=100&nSetPageSize=10&cSortFld=%D5%FD%CC%E2%C3%FB&B1=%BC%EC+%CB%F7;搜索的关键字name=txtTm,随后代码如下:

 

 

 

复制代码

$keyword="音乐";$post="txtWxlx=CN&hidWxlx=spanCNLx&txtPY=HZ&txtTm={$keyword}&txtLx=%25&txtSearchType=1&nMaxCount=100&nSetPageSize=20&cSortFld=%D5%FD%CC%E2%C3%FB&B1=%BC%EC+%CB%F7";

$url = "http://210.30.68.130/wxjs/tmjs.asp";

//查询地址

$ch = curl_init(); 

curl_setopt ($ch, CURLOPT_REFERER, "http://210.30.68.130/wxjs/tmjs_form.asp/ "); //模拟来源  

curl_setopt($ch, CURLOPT_URL, $url);//URL  

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_POST, 1);  //模拟POST  

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//POST内容  

curl_exec($ch);  

$output = curl_close($ch); 

echo $output;

 ?>

复制代码

 

 

但返回的页面总是显示没有相关内容,如果把关键字改成英语或者数字就可以正常显示了,于是想到这应该是编码问题,然后在post上面可以看到txtTm=%D2%F4%C0%D6经过查询得知这是url编码,汉字会进行转化,如果是英语则不会发生任何改变,于是就把头信息也加了部分内容进去如下:

 

$header = array();

$header[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:30.0) Gecko/20100101 Firefox/30.0';

$header[] = 'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';

$header[] = 'Connection: keep-alive';

$header[] = 'Content-Type:application/x-www-form-urlencoded';

curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header ); 

其实主要是$header[] = 'Content-Type:application/x-www-form-urlencoded';再次加载页面后如果是汉字显示还是没有相关的内容,随后想到了一个很简单的问题所在,php程序是utf8的,然而图书馆的网站是gb2312,好吧,再加上一句,$keyword =  iconv('UTF-8', 'GB2312', $keyword);再次加载成功,这个应该才是问题关键所在,然后我把头信息删除后添加一句$keyword = urlencode($keyword);再次加载也就是下面的代码:

 

复制代码

$keyword="世界";

$keyword =  iconv('UTF-8', 'GB2312', $keyword);

$keyword = urlencode($keyword);

$post="txtWxlx=CN&hidWxlx=spanCNLx&txtPY=HZ&txtTm={$keyword}&txtLx=%25&txtSearchType=1&nMaxCount=100&nSetPageSize=20&cSortFld=%D5%FD%CC%E2%C3%FB&B1=%BC%EC+%CB%F7";

$url = "http://210.30.68.130/wxjs/tmjs.asp";

//查询地址

$ch = curl_init();

curl_setopt ($ch, CURLOPT_REFERER, "http://210.30.68.130/wxjs/tmjs_form.asp/ "); //模拟来源  

curl_setopt($ch, CURLOPT_URL, $url);//URL  

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_POST, 1);  //模拟POST  

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//POST内容  

curl_exec($ch);  

$output = curl_close($ch); 

echo $output;

 ?>

复制代码

不出所料,正常显示了,随后就是页面内容的采集排版问题了。无非是正则了。(由于是校园网只能我们内网访问)

 

 



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template