首页 > 代码库 > [PHP] 调用微博API 发微博OAuth2.0

[PHP] 调用微博API 发微博OAuth2.0

在实际测试中出现很多问题,

第一就是按照文档调用ACCESS_TOKEN的时候费老劲啦,因为是编辑线上的,有好多中文空格,没有看出来!整了好久!

第二个就是在调用api发微博的时候出现乱码!必须把发送内容转化成URLcode的格式!

还有就是在index.php文件标红的$URL地址传输的时候问题!

下面看码吧,还有就是封装好的伪造表单提交curl.class.php的类!

  文件:weibo.php

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Document</title>
 6 </head>
 7 <body>
 8 <a href="https://api.weibo.com/oauth2/authorize?client_id=13465639&response_type=code&redirect_uri=http://www.lpcblog.com/index.php">
 9 微博登录</a>
10     
11 </body>
12 </html>

文件:index.php

 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);

文件(封装的模拟表单提交curl类)

 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 }

 

[PHP] 调用微博API 发微博OAuth2.0