php 自有文章同步微博

微博同步官方教程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;
}

最后---大结局

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值