標籤:touch 請求 efi ati user char json meta method
在實際測試中出現很多問題,
第一就是按照文檔調用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‘=>$code19 );20 $str = curl($url,$post_data,‘POST‘);21 $str = json_decode($str,true);22 //var_dump($str);//獲得access_token23 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‘=>$content31 );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