温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在PHP中利用CURL模拟登陆并上传文件

发布时间:2021-04-06 16:33:36 来源:亿速云 阅读:149 作者:Leah 栏目:开发技术

怎么在PHP中利用CURL模拟登陆并上传文件?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

具体如下:

<?php header('content-type:text/html;charset=gb2312'); //要注意你需要上传的网站服务器的运行环境,还要看它的请求是否被压缩和转码还有就是 //在框架中或者说php5.3以下的版本可以用@,但是其它的就只能用new CURLfile()函数来转化文件了 //注意你要发送的服务器的header头的结构和特殊参数,实在不行就自己构建一个。废话不多说,直接上代码。 function curl_form($post_data,$sumbit_url,$http_url,$cookie_file){   $headers = array();   $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';   $headers[] = 'Cache-Control: max-age=0';   $headers[] = 'Accept-Encoding: gzip, deflate';   $headers[] = 'Origin: http://my.***.com';   $headers[] = 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3';   $headers[] = 'Upgrade-Insecure-Requests: 1';   $headers[] = 'Content-Type: application/x-www-form-urlencoded';   $headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';   $headers[] = 'Connection: keep-alive'; //  $headers[] = 'Cookie: ASPSESSIONIDCCTCTQQC=KBGLPDKBIKDIDCBGFOKNMKOE';   //初始化   $ch = curl_init();   //设置变量   curl_setopt($ch, CURLOPT_URL, $sumbit_url);   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);   curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回   curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示   curl_setopt($ch, CURLOPT_REFERER, $http_url);   //表单数据,是正规的表单设置值为非0   curl_setopt($ch, CURLOPT_POST, 1);   curl_setopt($ch, CURLOPT_TIMEOUT, 30);   curl_setopt($ch,CURLOPT_PROXY,'127.0.0.1:8888'); //  curl_setopt($ch, CURLOPT_ENCODING, ""); //  curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');   curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));   //执行并获取结果   $output = curl_exec($ch);   if($output === FALSE)   {     echo "<br/>","cUrl Error:".curl_error($ch);   }else{     return $output;   }   //  释放cURL句柄   curl_close($ch); } $temp = array(); $temp['title'] = iconv( "utf-8", "gb2312//IGNORE" , "牛排店加盟"); $temp['ly'] = iconv( "utf-8", "gb2312//IGNORE" , "特色餐饮加盟"); $temp['classid'] = iconv( "utf-8", "gb2312//IGNORE" , "7159"); $temp['newssort'] = iconv( "utf-8", "gb2312//IGNORE" , "1"); $temp['panduan'] = iconv( "utf-8", "gb2312//IGNORE" , "0"); $temp['submit_button'] = iconv( "utf-8", "gb2312//IGNORE" , "发布"); $temp['addr'] = iconv( "utf-8", "gb2312//IGNORE" , "bjcanyin"); $temp['ContentBg'] = ""; $temp['newss'] = iconv( "utf-8", "gb2312//IGNORE" , htmlspecialchars_decode("<p>阿会计师的贺卡收到框架</p><p><img alt=\"\"  src=\" http://localhost/super/Uploads/img/2017-08-10/598c145a9527e.jpg\" style=\"height:243px; width:324px\" /></p>", ENT_QUOTES)); $cookie_file = dirname(__FILE__)."/jdzj.tmp"; $sumbit_url = "http://***/news/***.asp"; $http_url="http://***/news/***.asp?act=addok"; $img = curl_form($temp,$http_url,$sumbit_url,$cookie_file); var_dump($img);

关于怎么在PHP中利用CURL模拟登陆并上传文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI