<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->user_request(array('url' => $tmhOAuth->url('1.1/account/verify_credentials'))); $tmhOAuth->render_response();
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/friendships/create'), 'params' => array('screen_name' => 'tmhOAuth'))); $tmhOAuth->render_response();
echo 'rate limited. reset time is ' . $reset . PHP_EOL; echo 'sleeping for ' . $sleep . ' seconds'; sleep($sleep); } } require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $screen_name = 'tmhoauth'; $cursor = '-1'; $friends = array(); while (true) { if ($cursor == '0') { break; } echo '.'; $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/friends/list'), array('cursor' => $cursor, 'skip_status' => true, 'screen_name' => $screen_name)); check_rate_limit($tmhOAuth->response); if ($code == 200) { $data = json_decode($tmhOAuth->response['response'], true); $friends = array_merge($friends, $data['users']); $cursor = $data['next_cursor_str']; } else { $tmhOAuth->render_response(); break; } usleep(500000); } echo '@' . $screen_name . ' follows ' . count($friends) . ' users.' . PHP_EOL . PHP_EOL; foreach ($friends as $friend) { echo '@' . $friend['screen_name'] . ' (' . $friend['id_str'] . ')' . PHP_EOL; }
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $image = __DIR__ . DIRECTORY_SEPARATOR . 'photo.jpg'; $name = basename($image); $status = "Picture time"; $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update_with_media'), 'params' => array('media[]' => "@{$image};type=image/jpeg;filename={$name}", 'status' => $status), 'multipart' => true)); $tmhOAuth->render_response();
$name = basename($image); $status = "Picture time"; if (class_exists('CurlFile', false)) { $media = new CurlFile($image); } else { $media = "@{$image};type=image/jpeg;filename={$name}"; } // ref: https://dev.twitter.com/rest/public/uploading-media // first we post to the media upload endpoint // then we use the returned information to post to the tweet endpoint $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => 'https://upload.twitter.com/1.1/media/upload.json', 'params' => array('media' => $media), 'multipart' => true)); $tmhOAuth->render_response(); // response looks like this: /* { "media_id": 553656900508606464, "media_id_string": "553656900508606464", "size": 998865, "image": { "w": 2234, "h": 1873, "image_type": "image/jpeg" } } */ if ($code == 200) { $data = json_decode($tmhOAuth->response['response']); $media_id = $data->media_id_string; $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('media_ids' => $media_id, 'status' => $status))); } $tmhOAuth->render_response();
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('status' => 'MTBLS166-1H NMR and LC–MS/MS based urine metabolomic investigation of the subacute effects of HBCD in mice'))); $tmhOAuth->render_response();
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->apponly_request(array('method' => 'GET', 'url' => $tmhOAuth->url('1.1/search/tweets'), 'params' => array('q' => 'tmhoauth'))); $tmhOAuth->render_response();
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('status' => 'Something for the weekend'))); $tmhOAuth->render_response();
<?php // this example follows the guidance given on dev.twitter.com for obtaining an app-only bearer token require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $bearer = $tmhOAuth->bearer_token_credentials(); $params = array('grant_type' => 'client_credentials'); $code = $tmhOAuth->request('POST', $tmhOAuth->url('/oauth2/token', null), $params, false, false, array('Authorization' => "Basic {$bearer}")); if ($code == 200) { $data = json_decode($tmhOAuth->response['response']); if (isset($data->token_type) && strcasecmp($data->token_type, 'bearer') === 0) { $new_bearer = $data->access_token; echo 'Your app-only bearer token is: ' . $new_bearer . PHP_EOL; } } else { $tmhOAuth->render_response(); }
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->user_request(array('method' => 'GET', 'url' => $tmhOAuth->url('1.1/users/lookup'), 'params' => array('user_id' => array(777925), 'screen_name' => array('tmhoauth'), 'map' => 1))); $tmhOAuth->render_response();
<?php require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php'; $tmhOAuth = new tmhOAuthExample(); $code = $tmhOAuth->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $tmhOAuth->url('oauth/access_token', ''), 'params' => array('x_auth_username' => 'USERNAME', 'x_auth_password' => 'PASSWORD', 'x_auth_mode' => 'client_auth', 'send_error_codes' => true))); if ($code == 200) { $data = $tmhOAuth->extract_params($tmhOAuth->response['response']); if (isset($data['oauth_token'])) { echo 'OAuth Credentials for @' . $data['screen_name'] . ' (' . $data['user_id'] . ')' . PHP_EOL; echo 'Token: ' . $data['oauth_token'] . PHP_EOL; echo 'Secret: ' . $data['oauth_token_secret'] . PHP_EOL; // instead of printing these to screen you would typically save them to a database // if you do store the oauth_token and oauth_secret to a database it's best practice // store them with some kind of encryption. e.g. mcrypt_encrypt } } else { $tmhOAuth->render_response(); }
$url = sprintf("%s?%s", $url, http_build_query($data)); } } // Optional Authentication: curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($curl); return $result; } $StudyList = json_decode(CallAPI('GET', 'http://www.ebi.ac.uk/metabolights/webservice/study/list'), true); foreach ($StudyList['content'] as $study) { $StudyDetails = json_decode(CallAPI('GET', 'http://www.ebi.ac.uk/metabolights/webservice/study/' . $study), true); $releaseDate = $StudyDetails['content']['studyPublicReleaseDate']; $current = strtotime(date("Y-m-d")); $date = strtotime($releaseDate); $datediff = $date - $current; $difference = floor($datediff / (60 * 60 * 24)); if ($difference == 0) { var_dump('Today-' . $study); $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('status' => $study . ': ' . $StudyDetails['content']['title'] . ' http://www.ebi.ac.uk/metabolights/' . $study))); $tmhOAuth->render_response(); } else { if ($difference > 1) { var_dump('Future-' . $study); } else { var_dump('Published-' . $study); } } }