1
2
3
4
5
6
7
8
9 微博登录
10
11
12
1 php
2
3 include(‘curl.class.php‘);
4 $code=$_GET[‘code‘];
5 //echo $code;
6 $client_id="13465639";
7 $client_secret="c8d9fb98f8d56f85d04d1dd2c6323bde";
8 $grant_type="authorization_code";
9 $redirect_uri="http://www.lpcblog.com/index.php";
10
11 //$url="https://api.weibo.com/oauth2/access_token";
12 $url = "https://api.weibo.com/oauth2/access_token?client_id=".$client_id."&client_secret=".$client_secret."&grant_type=".$grant_type."&redirect_uri=".$redirect_uri."&code=".$code;
13 $post_data = array(
14 ‘grant_type‘=>$grant_type,
15 ‘client_id‘=>$client_id,
16 ‘client_secret‘=>$client_secret,
17 ‘redirect_uri‘=>$redirect_uri,
18 ‘code‘=>$code
19 );
20 $str = curl($url,$post_data,‘POST‘);
21 $str = json_decode($str,true);
22 //var_dump($str);//获得access_token
23
24 //$str[‘access_token‘]=‘2.00XhnHmG00jBVu8a2c4708694K2W2D‘;
25 //发送的微博内容
26 $content = "微博你好,有乱码,调试中!PHP";
27 $wei_url="https://api.weibo.com/2/statuses/update.json";
28 $weibo_data = array(
29 ‘access_token‘=>$str[‘access_token‘],
30 ‘status‘=>$content
31 );
32 $wei_str = curl($wei_url,http_build_query($weibo_data),‘POST‘);
33 $wei_str = json_decode($wei_str,true);
34 var_dump($wei_str);
1 php
2
3 $COOKIE_file = tempnam(‘./temp‘,‘COOKIE‘); //创建COOKIE文件保存的位置
4
5 function curl($url,$data=array(),$method,$setCOOKIE=false,$COOKIE_file=false){
6 $ch = curl_init();//1.初始化
7 curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
8 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
9 //4.参数如下禁止服务器端的验证
10 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
11 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
12 //伪装请求来源,绕过防盗
13 //curl_setopt($ch,CURLOPT_REFERER,"http://wthrcdn.etouch.cn/");
14 //配置curl解压缩方式(默认的压缩方式)
15 curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Accept-Encoding:gzip‘));
16 curl_setopt($ch, CURLOPT_ENCODING, "gzip");
17 curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[‘HTTP_USER_AGENT‘]);
18 //指明以哪种方式进行访问,利用$_SERVER[‘HTTP_USER_AGENT‘],可以获取
19 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
20 curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
21 if($method=="POST"){//5.post方式的时候添加数据
22 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
23 }
24 if($setCOOKIE==true){
25 //如果设置要请求的COOKIE,那么把COOKIE值保存在指定的文件中
26 curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIE_file);
27 }else{
28 //就从文件中读取COOKIE的信息
29 curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIE_file);
30 }
31 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
32 $tmpInfo = curl_exec($ch);//获取html内容
33 if (curl_errno($ch)) {
34 return curl_error($ch);
35 }
36 curl_close($ch);
37 return $tmpInfo;
38 }