您的位置:首页 > 编程语言 > PHP开发

PHP模拟post添加数据

2014-11-26 00:00 309 查看
摘要: PHP模拟post添加数据

科普说明一:
需要Referer的采集
对于一些程序,它可能判断来源网址,如果发现referer不是自己的网站,则拒绝访问,这时候,我们就需要添加CURLOPT_REFERER参数,模拟来路,使得程序能够正常采集。
curl_setopt($ch, CURLOPT_REFERER, $refer); //来路模拟 //$refer== http://www.hao123.com/index.php?a/b/2

header("content-Type: text/html; charset=UTF-8");
/**

curl请求

@param array $data 要请求的array数组

@param str $url 请求的地址

@param str $method 大写 POST GET

@param array $headers 扩展包头信息

@return string

*/
function curl($data, $url, $method = 'POST', $headers = array()) {
$ch = curl_init();
$method_upper = strtoupper($method);
if ($method_upper == 'POST') {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
} else {
$url = $url . (strpos($url, '?') ? '&' : '?') . (is_array($data) ? http_build_query($data) : $data);
curl_setopt($ch, CURLOPT_URL, $url);
}
$refer= 'http://www.hao123.com/index.php?a/b/2';
$ana='Hm_lvt_bfee58c2608e7b38ae1e577091933288=1416972533; sessionId=2143F4829A5246D496FBCC4C20F5C2F3; city=beijing; account=hao123@163.com; Hm_lvt_e0ce3e3027e784f5f6347af477dce717=1416906174,1416906818,1416965936; Hm_lpvt_e0ce3e3027e784f5f6347af477dce717=1416972673; JSESSIONID=2143F4829A5246D496FBCC4C20F5C2F3; Hm_lpvt_bfee58c2608e7b38ae1e577091933288=1416972533'
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method_upper);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, refer); //来路模拟
curl_setopt($ch, CURLOPT_COOKIE, $ana); //存储提交后得到的cookie数据
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //SSL 报错时使用
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //SSL 报错时使用
if ($headers) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
exit(curl_error($ch));
}
curl_close($ch);
return $tmpInfo;
}

$head = array(
'Host' => 'm.hao123.com',
'X-Requested-With' => 'XMLHttpRequest',
'CLIENT-IP'=>'202.103.229.40',
);
$result = curl('id=&province=%E7%A6%8F%E5%BB%BA%E7%9C%81&city=%E7%A6%8F%E5%B7%9E%E5%B8%82&district=%E9%BC%93%E6%A5%BC%E5%8C%BA&contactName=aaa&contactPhone=15877878898&address=sdfsdfsdfsdf&community=&area=30', 'http://m.hao123.com/location/save', 'POST', $head);

print_r($result);

参考:http://www.softwareishard.com/blog/planet-mozilla/firebug-tip-resend-http-request/

http://www.cnblogs.com/Zjmainstay/p/php-curl.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: