Skip to content

Instantly share code, notes, and snippets.

@mikamboo
Last active December 24, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikamboo/6786943 to your computer and use it in GitHub Desktop.
Save mikamboo/6786943 to your computer and use it in GitHub Desktop.
PHP Pson lib + Twitter Api sample
<?php
/**
* Sample usage of Pson lib with twitter api v1.1
* Copyrights mikangali-labs 2013 - https://github.com/mikangali-labs
*
* Thanks to James Mallison (https://github.com/J7mbo) for original TwitterAPIExchange
*/
/**
* Include Wrapper for Twitter API v1.1
* @see: https://github.com/mikamboo/twitter-api-php
*/
require_once('twitter-api-php\TwitterAPIExchange.php');
/**
* Include Pson library
* @see https://github.com/mikangali-labs/Pson
*/
require_once('Pson\src\Mikangali\Pson\Pson.php');
use Mikangali\Pson\Pson;
/**
* Sample tweet model
*/
class Tweet {
public $id;
public $text;
public $created_at;
private $source;
/**
* @FieldClass('User')
*/
public $user;
}
class User {
public $id;
public $name;
public $screen_name;
}
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_KEY_SECRET",
'curl_ssl_verify' => false, // avoid CA certs SSL certificate problem on localhost
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/statuses/home_timeline.json';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
//-- Get json from twitter api
$jsonfeed = $twitter->buildOauth($url, $requestMethod)->performRequest();
$pson = new Pson();
// Un-serialize json array
$tweets = $pson->fromJsonArray($jsonfeed,'Tweet');
echo "<pre>"; print_r($tweets);
// Serialize single object
$json = $pson->getJson($tweets[0], "user");
echo "<pre>"; print_r($json);
@yanntheret
Copy link

At line 14, the require_once() should points to the Pson lib path, not to an absolute path...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment