php利用curl登录页面并post提交数据上传文件

2015-11-26 寒川 网页编程

好久没写博客了,不知道的人或许会以为博客已经停止更新了,其实不然,确实最近比较忙。偶尔有空也只是上来删删一些垃圾留言。今天特地总结一下php的curl,显示用curl登录目标网站,接着携带cookie提交数据,用post方式提交,接着再上传一张本地图片。贴出这比较low的代码备用。闲话就不多说了,下面看招:

<?php
function login(){
	$url = 'http://xx.com/login.php';//登录地址
	$data = array(
		'username'=>'用户',//用户名,注意与原表单一致
		'password'=>'password'//用户密码,注意与原表单一致
	);
	$cookie_file = tempnam('./tmp','cookie');
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36');  
	curl_setopt($ch, CURLOPT_HEADER, 0);  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
	curl_setopt($ch, CURLOPT_MAXREDIRS, 1);  
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
	curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
	curl_setopt($ch, CURLOPT_POST, 1);  
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
	curl_exec($ch);  
	curl_close($ch);
	return $cookie_file;
}

function post(){
	$data = array(
		'name'=>1,//注意与原表单一致
		'test'=>2,//注意与原表单一致
	);
	$cookie = login();
	$url="http://xx.com/xx.php";//数据提交地址
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36');
	curl_setopt($ch, CURLOPT_HEADER, 0);  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);  
	$str = curl_exec($ch);  
	curl_close($ch);
	unlink($cookie);
	return $str;
}


function uploadfile(){
	$cookie = login();
	$fields['data'] = 'data';//注意与原表单一致
	$fields['file'] = '@/www/web/xx.jpg;type=image/jpeg;filename=xx.jpg';//注意与原表单一致
	$url='http://xx.com/upload.php';//数据提交地址
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36');
	curl_setopt($ch, CURLOPT_HEADER, 0);  
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt ($ch,CURLOPT_REFERER,'http://59.151.121.119:8082/mss/uploadHkrtImage.jhtml');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);  
	$str =  curl_exec($ch);  
	curl_close($ch);
	unlink($cookie);
	return $str;
}
具体使用代码的时候记得注释部分修改成自己的。

标签: php curl 模拟登陆 cookie curl上传

评论(0) 浏览(5799)

php curl写个新浪发sina微博的API接口

2010-7-31 寒川 网页编程

一直没得到新浪微博API Key,自己用php curl写个新浪发sina微博的API接口,发出来分享,和《封装一个php发QQ微博的类 》是一个原理,几乎一模一样了。废话不多说,直接上菜:
<?php

/*

*How to use?

*publish("username","password","have a test.");

*Copy Right 寒川

*URL:http://huikon.cn

*/

function publish($username, $password, $content=''){//发布
    $referURL = 'http://t.sina.com.cn';
    $url = 'http://t.sina.com.cn/mblog/publish.php';
    $fields = array(
    'content'=>urlencode($content) ,
    );
    $fields_string = '';
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&amp;' ; }
    rtrim($fields_string ,'&amp;') ;
    $fields_string = substr($fields_string, 0,-5);
   
    $cookie_jar = login($username, $password);

    $curl = curl_init($url) ;
    curl_setopt($curl, CURLOPT_POST,count($fields)) ;
    curl_setopt($curl, CURLOPT_POSTFIELDS,$fields_string) ;
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($curl, CURLOPT_REFERER, $referURL);
    curl_exec($curl);
    curl_close($curl);
    unlink($cookie_jar);
}

function login($username, $password){//登录
    $loginURL = 'https://login.sina.com.cn/sso/login.php?username='.$username.'&password='.$password.'&returntype=TEXT';
    $curl = curl_init($loginURL);
    $cookie_jar = tempnam('.', 'cookie');
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_exec($curl);
    curl_close($curl);
    return $cookie_jar;
}

?>

标签: php api curl 新浪微博 微博API

评论(4) 浏览(14312)

Powered by emlog 蜀ICP备12030225号

川公网安备 51042102000001号

sitemap