分享

Face book API,Twitter API 及其调用

 陈永正的图书馆 2017-08-30

1.Facebook

 

1.)http://www./ 注册账号

2.)添加应用application

  1. Go to http://www./developers/createapp.php to create a new application.
  2. Enter a name for your application in the Application Name field.
  3. Accept the Developer Terms of Service , then click Save Changes .
  4. On the Basic tab, keep all of the defaults.
  5. Take note of the API Key , you'll need this shortly.
  6. Click the Connect tab. Set Connect URL to the top-level directory of the site where you plan to implement Facebook Connect (this is usually your domain, like http://www. , but could also be a subdirectory).
  7. You should include a logo that appears on the Facebook Connect dialog. Next to Facebook Connect Logo , click Change your Facebook Connect logo and browse to an image file. The logo can be up to 99 pixels wide by 22 pixels tall, and must be in JPG, GIF, or PNG format. (Filename cannot be 'icon')
  8. If you plan to implement Facebook Connect across a number of subdomains of your site (for example, foo. and bar.), you need to enter a Base Domain (which would be in this case). Specifying a base domain allows you to make calls using the PHP and JavaScript client libraries as well as get and store session information for any subdomain of the base domain. For more information about subdomains, see Supporting Subdomains In Facebook Connect .
  9. Click Save Changes .

3.)添加方法

http://www./authorize.PHP?api_key=72987fb4baab2fa37308f9f551a2c72d&v=1.0&ext_perm=notes_create
http://www./authorize.php?api_key=72987fb4baab2fa37308f9f551a2c72d&v=1.0&ext_perm=notes_delete
http://www./authorize.php?api_key=72987fb4baab2fa37308f9f551a2c72d&v=1.0&ext_perm=notes_edit

调用方法 加权限

 

4.)编写代码:

$appapikey = '****';
$appsecret = '******';
$user_id= '*******';
$title = 'windy1111';
$content = 'wang2222';

echo "publish content:";
$facebook = new Facebook($appapikey, $appsecret);
$facebook->api_client->users_hasAppPermission('create_note',$user_id);

//添加Notes

$info = $facebook->api_client->notes_create($title,$content,$user_id);
var_dump($info);

编辑Notes

$status = $facebook->api_client->notes_edit($messageId, $title, $content, $userId);

删除Notes

$status = $facebook->api_client->notes_delete($messageId, $userId);

 

2.Twitter

 

1.)注册用户 https://twitter.com/

2.)编写代码:

 

发送信息到主页:

public static function twitterSendMessage($message, $username, $password){

         //echo "windy";
         $host = 'http://twitter.com/statuses/update.xml?status=[[MESSAGE]]';
        /**********************
        * TWITTER SEND MESSAGE
        ***********************/
        $host = str_replace("[[MESSAGE]]", urlencode(stripslashes(urldecode($message))), $host);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
       
       
       
        $result = curl_exec($ch);
        // Look at the returned header
        $resultArray = curl_getinfo($ch);
        //var_dump($resultArray);
        curl_close($ch);

        return $result;
    }

返回MessageId

 

public static function getTwitterMessageId($xmlData){
        $fileName = "tempTwitter_".time() . ".xml";
        $filePath = SF_ROOT_DIR."/web/".$fileName;
        $fp = @fopen("$filePath","w");
        @fwrite($fp,$xmlData);
        fclose($fp);
        $dom = new DOMDocument();
        $dom->load($filePath);
        $x = $dom->documentElement;
        $nodeArray = array();
        foreach ($x->childNodes AS $item){
          $nodeArray[$item->nodeName] = $item->nodeValue;
        }
        $twitterId = $nodeArray['id'];
        unlink($filePath);
        return $twitterId;
    }

 

 

删除信息:

public static function twitterDeleteMessage($messageId, $username, $password){

        $host = 'http://twitter.com/statuses/destroy/'.$messageId.'.xml';
        /**********************
        * TWITTER SEND MESSAGE
        ***********************/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        $result = curl_exec($ch);
        // Look at the returned header
        $resultArray = curl_getinfo($ch);
        curl_close($ch);

        return $resultArray;
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多