curl实现表单数据的提交
请问老师 这个怎么实现post提交表单数据,比如某个评论?
请问老师 这个怎么实现post提交表单数据,比如某个评论?
2016-06-06
ajax处理,将需要提交的数据获取并提交给后台,后台处理这个数据。设置CURLOPT_HTTPHEADER头部信息发送给原来站点的地址,直接送上代码
/** * 模拟登录 * @param string $url 请求的登录url * @param [type] $cookie cookie名称 此处必须使用绝对路径 * @param string $data 发送的字段 * @return string */ function login_get($url,$cookie,$data = "") { $curlobj = curl_init();// 初始化 curl_setopt($curlobj, CURLOPT_URL, $url);// 设置访问网页的URL curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true);// 执行之后不直接打印出来 curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); date_default_timezone_set('PRC'); // 使用Cookie时,必须先设置时区 curl_setopt($curlobj, CURLOPT_COOKIESESSION, TRUE); curl_setopt($curlobj, CURLOPT_COOKIEJAR, $cookie); //连接时把获得的cookie存为文件 curl_setopt($curlobj, CURLOPT_COOKIE, session_name().'='.session_id()); curl_setopt($curlobj, CURLOPT_HEADER, 0); curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlobj, CURLOPT_POST, 1); curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data) )); $rs = curl_exec($curlobj);//执行 curl_close($curlobj); return $rs; } /** * 获取url页的内容 * @param string $url 需要获取内容页面的URl * @param [type] $cookie cookie名称 * @return string */ function get_content($url, $cookie,$data ="") { $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, $url); curl_setopt($curlobj, CURLOPT_HEADER, 0); curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlobj, CURLOPT_TIMEOUT, 300); date_default_timezone_set('PRC'); // 使用Cookie时,必须先设置时区 curl_setopt($curlobj, CURLOPT_COOKIEFILE, $cookie); //读取cookie curl_setopt($curlobj, CURLOPT_POST, 1); curl_setopt($curlobj, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curlobj, CURLOPT_POSTFIELDS, $data); curl_setopt($curlobj, CURLOPT_HTTPHEADER, array("application/x-www-form-urlencoded; charset=utf-8", "Content-length: ".strlen($data) )); $rs = curl_redir_exec($curlobj); //执行cURL抓取页面内容 curl_close($curlobj); return $rs; }
举报