Skip to content

Instantly share code, notes, and snippets.

@kaz
Created July 23, 2015 13:05
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 kaz/0b1238c3ac1ed6452504 to your computer and use it in GitHub Desktop.
Save kaz/0b1238c3ac1ed6452504 to your computer and use it in GitHub Desktop.
<?php
if(!array_key_exists("q", $_GET)) exit("no query");
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_URL => "https://twitter.com/i/search/timeline?" . http_build_query([
"q" => $q = $_GET["q"],
"src" => "typd",
"vertical" => "default",
"include_available_feature" => 1,
"include_entities" => 1,
"last_note_ts" => time(),
"max_position" => array_key_exists("c", $_GET) ? $_GET["c"] : ""
])
]);
$resp = json_decode(curl_exec($ch), true);
curl_close($ch);
preg_match_all('@data-user-id="(\d+)">.+?fullname.+?>(.+?)</strong>.+?href="/(.+?)/status/(\d+)".+?data-time="(\d+)".+?(<p.+?)</p>@s', $resp["items_html"], $tweets, PREG_SET_ORDER);
foreach($tweets as &$entry){
$entry = [
"UserID" => (int)$entry[1],
"FullName" => $entry[2],
"ScreenName" => $entry[3],
"StatusID" => (int)$entry[4],
"Time" => $entry[5],
"Status" => preg_replace("@<.+?>@", "", preg_replace('@<a href="(http.+?)".+?/a>@', "$1", $entry[6]))
];
}
$c = $resp["min_position"];
$next = "http://{$_SERVER["SERVER_NAME"]}{$_SERVER["SCRIPT_NAME"]}?" . http_build_query(compact("q", "c"));
header("Content-Type: application/json; charset=utf-8");
echo(json_encode(compact("next", "tweets")));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment