Kaixin.com Account logon and api operations-PHP source code

Source: Internet
Author: User
Kaixin.com Account logon and api operations are performed on Kaixin.com Account logon and api operations, using oauth 2.0
The official SDKs are too large, which is simplified by myself. they provide simple Account logon, personal information retrieval, and sharing functions, if you need other functions, you can add them according to the official api documentation.

Kaixin. php

 client_id=$client_id;        $this->client_secret=$client_secret;        $this->access_token=$access_token;    }     function login_url($callback_url, $scope=''){        $params=array(            'response_type'=>'code',            'client_id'=>$this->client_id,            'redirect_uri'=>$callback_url,            'scope'=>$scope        );        return 'http://api.kaixin001.com/oauth2/authorize?'.http_build_query($params);    }     function access_token($callback_url, $code){        $params=array(            'grant_type'=>'authorization_code',            'code'=>$code,            'client_id'=>$this->client_id,            'client_secret'=>$this->client_secret,            'redirect_uri'=>$callback_url        );        $url='https://api.kaixin001.com/oauth2/access_token';        return $this->http($url, http_build_query($params), 'POST');    }     function access_token_refresh($refresh_token){        $params=array(            'grant_type'=>'refresh_token',            'refresh_token'=>$refresh_token,            'client_id'=>$this->client_id,            'client_secret'=>$this->client_secret        );        $url='https://api.kaixin001.com/oauth2/access_token';        return $this->http($url, http_build_query($params), 'POST');    }     function me(){        $params=array();        $url='https://api.kaixin001.com/users/me.json';        return $this->api($url, $params);    }     function records_add($content, $picurl=''){        $params=array(            'content'=>$content        );        if($picurl!='')$params['picurl']=$picurl;        $url='https://api.kaixin001.com/records/add.json';        return $this->api($url, $params, 'POST');    }     function records_me($num=10, $start=0){        $params=array(            'start'=>$start,            'num'=>$num        );        $url='https://api.kaixin001.com/records/me.json';        return $this->api($url, $params);    }     function comment_list($id, $uid, $num=10, $start=0){        $params=array(            'objtype'=>'records',            'objid'=>$id,            'ouid'=>$uid,            'start'=>$start,            'num'=>$num        );        $url='https://api.kaixin001.com/comment/list.json';        return $this->api($url, $params);    }     function forward_list($id, $uid, $num=10, $start=0){        $params=array(            'objtype'=>'records',            'objid'=>$id,            'ouid'=>$uid,            'start'=>$start,            'num'=>$num        );        $url='https://api.kaixin001.com/forward/list.json';        return $this->api($url, $params);    }     function like_show($id, $uid, $num=10, $start=0){        $params=array(            'objtype'=>'records',            'objid'=>$id,            'ouid'=>$uid,            'start'=>$start,            'num'=>$num        );        $url='https://api.kaixin001.com/like/show.json';        return $this->api($url, $params);    }     function api($url, $params, $method='GET'){        $params['access_token']=$this->access_token;        if($method=='GET'){            $result=$this->http($url.'?'.http_build_query($params));        }else{            $result=$this->http($url, http_build_query($params), 'POST');        }        return $result;    }     function http($url, $postfields='', $method='GET', $headers=array()){        $ci=curl_init();        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);         curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);        curl_setopt($ci, CURLOPT_TIMEOUT, 30);        if($method=='POST'){            curl_setopt($ci, CURLOPT_POST, TRUE);            if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);        }        $headers[]="User-Agent: kaixinPHP(piscdong.com)";        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);        curl_setopt($ci, CURLOPT_URL, $url);        $response=curl_exec($ci);        curl_close($ci);        $json_r=array();        if($response!='')$json_r=json_decode($response, true);        return $json_r;    }}

Config. php

 

Index. php

 Me (); var_dump ($ result ); /** // use refresh token to refresh the access token $ result = $ kaixin-> access_token_refresh ($ _ SESSION ['kaixin _ R']) after the access token expires. var_dump ($ result); ** // release record $ img = 'http: // www.baidu.com/img/baidu_sylogo1.gif'; $ result = $ kaixin-> records_add ('record content ', $ img); var_dump ($ result); **/} else {// Generate a logon link $ kaixin = new kaixinPHP ($ kaixin_k, $ kaixin_s ); $ login_url = $ kaixin-> login_url ($ callback_url, $ scop E); echo 'click to enter the authorization page ';}?>

Calllback. php

 Access_token ($ callback_url, $ _ GET ['code']);} if (isset ($ result ['Access _ token']) & $ result ['Access _ token']! = '') {Echo 'authorization completed. please record
Access token:
Refresh token:'; // Save the logon information. In this example, use session to save $ _ SESSION ['kaixin _ t'] = $ result ['Access _ token']; // access token $ _ SESSION ['kaixin _ R'] = $ result ['refresh _ token']; // refresh token} else {echo 'authorization failed ';} echo'
Return ';?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.