微博同步官方教程https://open.weibo.com/wiki/2/statuses/share
我觉得写这个文档教程的人是个傻逼~
第一步 获取code
//访问此方法返回的URL 进行登录微博 拿到code
function get_wb_code(){
$url="https://api.weibo.com/oauth2/authorize";
$client_id='';
$client_secret='';
$redirect_uri='http://'.$_SERVER['SERVER_NAME'].'/home/Auth/code';
return $url."?client_id=".$client_id.'&redirect_uri='.$redirect_uri;
}
第二部 通过code 拿到 access_token
function get_wb_token($code){
$url="https://api.weibo.com/oauth2/access_token";
$client_id='';
$client_secret='';
$grant_type='authorization_code';
$redirect_uri='http://'.$_SERVER['SERVER_NAME'].'/home/Auth/authorization_code';
$data=['client_id'=>$client_id,'client_secret'=>$client_secret,'grant_type'=>$grant_type,'code'=>$code,'redirect_uri'=>$redirect_uri];
$urls=$url."?client_id=".$client_id."&client_secret=".$client_secret."&grant_type=".$grant_type."&code=".$code."&redirect_uri=".$redirect_uri;
$r= curl_post_gettoken($urls,[]);
return $r;
}
//post 请求
function curl_post_gettoken($urls){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL =>$urls,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => [],
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
return $response;
}
第三步 通过access_token发布文章同步至微博
/**
*$content 文本内容 加上安全域名 并urlencode
*$pic 本地图片路径
*/
function curl_post($content,$pic, $access_token=''){
$data = array();
$url = "https://api.weibo.com/2/statuses/share.json";
$params['status'] = $content;
$params['access_token'] = $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$filename=CMF_ROOT.'public'.$pic;
$file=getimagesize($filename);
$filetype=$file['mime'];
$file = new \CURLFile($filename,$filetype, $file['name']);
$params['pic'] = $file;
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
$result = curl_exec($ch);
curl_close($ch);
if($data === null){
$this->dump('请求数据失败.', 2);
}else{
try{
$data = json_decode($result, true);
}catch(\Exception $e){
$this->dump('parse error.', 2, $e);
}
}
return $data;
}
最后---大结局