您好,欢迎访问一九零五行业门户网

用CURL提交POST数据

什么是url: curl是利用url语法在命令行方式下工作的文件传输工具。 php教程实例: ?php set_time_limit(0); @date_default_timezone_set('asia/shanghai'); function curlrequest($url,$postfield,$proxy=){ $proxy=trim($proxy); $user_agent =mozilla/
什么是url:
curl是利用url语法在命令行方式下工作的文件传输工具。
php教程实例:
set_time_limit(0);
@date_default_timezone_set('asia/shanghai');
function curlrequest($url,$postfield,$proxy=){
$proxy=trim($proxy);
$user_agent =mozilla/4.0 (compatible; msie 6.0; windows nt 5.1; sv1);
$ch = curl_init();// 初始化curl句柄
if(!empty($proxy)){
curl_setopt ($ch, curlopt_proxy, $proxy);//设置代理服务器
}
curl_setopt($ch, curlopt_url, $url); //设置请求的url
//curl_setopt($ch, curlopt_failonerror, 1); // 启用时显示http状态码,默认行为是忽略编号小于等于400的http信息
//curl_setopt($ch, curlopt_followlocation, 1);//启用时会将服务器服务器返回的“location:”放在header中递归的返回给服务器
curl_setopt($ch, curlopt_returntransfer,1);// 设为true把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, curlopt_post, 1);//启用post提交
curl_setopt($ch, curlopt_postfields, $postfield); //设置post提交的字符串
//curl_setopt($ch, curlopt_port, 80); //设置端口
curl_setopt($ch, curlopt_timeout, 25); // 超时时间
curl_setopt($ch, curlopt_useragent, $user_agent);//http请求user-agent:头
//curl_setopt($ch,curlopt_header,1);//设为true在输出中包含头信息
//$fp = fopen(example_homepage.txt, w);//输出文件
//curl_setopt($ch, curlopt_file, $fp);//设置输出文件的位置,值是一个资源类型,默认为stdout (浏览器)。
curl_setopt($ch,curlopt_httpheader,array(
'accept-language: zh-cn',
'connection: keep-alive',
'cache-control: no-cache'
));//设置http头信息
$document = curl_exec($ch); //执行预定义的curl
$info=curl_getinfo($ch); //得到返回信息的特性
//print_r($info);
if($info[http_code]==405){
echo bad proxy {$proxy}\n;//代理出错
exit;
}
//curl_close($ch);
return $document;
}
//请求url
$url=http://example.cn/getinfo.php;
//post提交数据,可用httpwatch查看
$postfield=username=test&year=2008&password=123456&submit=%cc%e1%bd%bb;
//代理服务器
$proxy = '';
//请求
$str=curlrequest($url,$postfield,$proxy);
//输出结果
echo $str;
原文地址:用curl提交post数据, 感谢原作者分享。
其它类似信息

推荐信息