AyrShare.php 6.4 KB
<?php

namespace App\Helper;

use GuzzleHttp\Client;

/**
 * @name: ayr_share社交绑定
 */
class AyrShare
{
    public $path = 'https://app.ayrshare.com/api';
    //api_key
    public $api_key = 'G8GQW3X-XBTMGXW-QPDDZ9A-WE1Z5SB';
    //系统设置
    public $config = [

    ];
    //profile_key
    public $profile_key = '';
    //设置请求头
    public $headers = [
        'Authorization' => 'Bearer ',
        'Content-Type' => 'application/json',
    ];

    /**
     * @name :参数构建
     */
    public function __construct()
    {
        $this->headers['Authorization'] = $this->headers['Authorization'].$this->api_key;
    }

    /**
     * @name   :获取过去30天发布的历史记录(1-30)
     * @author :lyh
     * @method :get
     * @time   :2023/5/5 10:00
     */
    public function get_analytics_links($to_day){
        $last_days = (string)$to_day;
        $url = $this->path.'/analytics/links?lastDays='.$last_days;
        return $this->http_click('get',$url);
    }

    /**
     * @name   :(通过 Ayrshare 获取给定帖子的实时分析,例如点赞、印象、转推等)post_analytics
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 11:56
     */
    public function post_analytics($id){
        $param = [
            'id'=>$id,
            'platforms' => ['facebook', 'instagram', 'twitter', 'linkedin', 'pinterest', 'youtube', 'tiktok'],
        ];
        $url = $this->path.'/api/analytics/post';
        return $this->http_click('post', $url, $param);
    }
    /**
     * @name   :(获取特定用户个人资料)analytics_post
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 10:43
     */
    public function post_analytics_social(){
        $post_data = [
            'platforms' => ['facebook', 'instagram', 'twitter', 'linkedin', 'pinterest', 'youtube', 'tiktok'],
        ];
        $url = $this->path.'/api/analytics/social';
        return $this->http_click('post',$url,$post_data);
    }

    /**
     * @name   :(设置自动计划)post_schedule_set
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 13:58
     */
    public function post_schedule_set($data){
        $param = [
            'schedule'=>["13:05Z", "20:14Z"],
            'title'=>$data['title'],
        ];
        $url = $this->path.'/api/auto-schedule/set';
        return $this->http_click('post',$url,$param);
    }

    /**
     * @name   :(删除自动计划)delete_schedule
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 14:04
     */
    public function delete_schedule($data){
        $param = [
            'title'=>$data['title'],
        ];
        $url = $this->path.'/api/auto-schedule/delete';
        return $this->http_click('delete',$url,$param);
    }

    /**
     * @name   :(列出自动计划)get_schedule_list
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 14:08
     */
    public function get_schedule_list(){
        $url = $this->path.'/api/auto-schedule/list';
        return $this->http_click('get',$url);
    }

    /**
     * @name   :(发布到用户个人资料)post_user
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 15:00
     */
    public function post_profiles($data){
        $param = [
            'post'=>$data['post'],
            'platforms'=>$data['platforms'],
            'profileKey'=>$this->profile_key,
            'mediaUrls'=>$data['mediaUrls']
        ];
        $url = $this->path.'/api/post';
        return $this->http_click('post',$url,$param);
    }

    /**
     * @name   :(创建子账户配置文件)post_create_profiles
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 15:16
     */
    public function post_create_profiles($data){
        $param = [
            'title'=>$data['title'],
        ];
        $url = $this->path.'/api/profiles/profile';
        return $this->http_click('post',$url,$param);
    }
    /**
     * @name   :(删除子账户配置文件)post_create_profiles
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 15:16
     */
    public function deleted_create_profiles($data){
        $param = [
            'title'=>$data['title'],
            'profileKey'=>$this->profile_key,
        ];
        $url = $this->path.'/api/profiles/profile';
        return $this->http_click('delete',$url,$param);
    }

    /**
     * @remark :支持 Instagram, Facebook, Twitter, YouTube.
     * @name   :(修改子账户)put_update_profiles
     * @author :lyh
     * @method :post
     * @time   :2023/5/5 17:00
     */
    public function put_update_profiles($data){
        $param = [
            'title'=>$data['title'],
            'profileKey'=>$this->profile_key,
        ];
        $url = $this->path.'/api/profiles/profile';
        return $this->http_click('put',$url,$param);
    }

    /**
     * @name   :(通过用户名获取社交账号)get_brand_user
     * @author :lyh https://app.ayrshare.com/api/brand/byUser
     * @method :post
     * @time   :2023/5/5 17:32
     */
    public function get_brand_user($param){
        //[[platforms,'instagram','user']],[platforms,'twitter',user]]
        $str = '';
        foreach ($param as $k =>$v){
            $str .= $v[0][$k].'='.$v[1];
        }
        $url = $this->path.'/api/brand/byUser?'.$str;
        return $this->http_click('get',$url);
    }
    public function get_profiles(){
//        $param = [
//            'title'=>$data['title'],
//            'refId'=>$data['refId'],
//            'profileKey'=>$this->profile_key,
//        ];
        $url = $this->path.'/api/profile';
        return $this->http_click('get',$url);
    }

    public function api_user(){
        $post_data = [
            'platforms' => ['facebook', 'instagram', 'twitter', 'linkedin', 'pinterest', 'youtube', 'tiktok'],
        ];
        $url = $this->path.'/api/user';
        return $this->http_click('post',$url,$post_data);
    }


    /**
     * 发送http post,get,put,delete请求
     * @param type $url
     * @param type $post_data
     */
    function http_click($method = 'post',$url, $param = [],$header = [])
    {
        if(!empty($param)){
            $post_data['json'] = $param;
        }
        $post_data['headers'] = !empty($header) ? $header : $this->headers;
        $client = new Client();
        try {
            $res = $client->request(strtoupper($method), $url, $post_data)->getBody()->getContents();
            return $res;
        } catch (\Exception $e) {
            return json_encode(["status"=>"fail","message"=>$e->getMessage()]);
        }
    }
}