Searching for tweets by date using Twitterapiexchange - php

I am attempting to search tweets within a given date range, using the twitterapiexchange wrapper.
Here is my code
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xx",
'oauth_access_token_secret' => "xx",
'consumer_key' => "xx",
'consumer_secret' => "xx"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET"
$getfield = "?q=from:manutd&until:2018-01-20&since:2018-01-01";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if(array_key_exists("errors", $string)) {echo "<h3>Sorry, there was a problem.</h3>
<p>Twitter returned the following error message:</p><p><em>".$string[errors][0]
["message"]."</em></p>";exit();}
foreach($string as $items)
{
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet: ". $items['full_text']."<br />";
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br />";
echo "Friends: ". $items['user']['friends_count']."<br />";
echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
}
This returns the latest tweets from the specified user, the date field appears to be ignored.
Is it possible to search by date using the API ?

The search API supports this, but you have to assemble your query like so:
https://api.twitter.com/1.1/search/tweets.json?q=from%3Atwitterdev&result_type=mixed&count=2
Refer: https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
The query string above is the urlencoded form of:
so you will pass like
https://api.twitter.com/1.1/search/tweets.json?q=%3Afrom:manutd&until:2018-01-20&since:2018-01-01
The list of search operators can be found here.

Related

Php - Showing youtube videos in a table

I'm working with the youtube v3 api. After a few tests, I realized that I need some help. When I'm trying to display the xml content, I'm just getting null values. Can anybody help me?
If you want to see the xml:
https://www.youtube.com/feeds/videos.xml?channel_id=$channelid
And my code is:
$xml=simplexml_load_file("videos.xml");
foreach($xml as $content) {
echo $content->title . "<br>";
echo $content->link['href'] . "<br>";
}
Xml that I want to display:
<entry>
Video ID <yt:videoId>Q4vSZA_8kYY</yt:videoId>
Video title <title>¡Trailer del canal! CBPrductions</title>
Upload date <published>2016-01-14T07:37:03+00:00</published>
<media:group>
Description <media:description>
LIKE PORQUE LO DIGO YO _ Suscribete!: https://www.youtube.com/user/SpanishCBProductions Dale a LIKE a mi página Facebook: https://www.facebook.com/SpanishCBProductions Sigueme en TWITTER!: https://twitter.com/CcristianN3 Y en mi poco sexy INSTAGRAM: http://instagram.com/ccristiann3/
</media:description>
</media:group>
</entry>
I think you can register the namespace and use xpath.
Then for the 'media' and the 'yt' you can get the children by passing the namespace.
If you want to display the first entry:
$url = 'https://www.youtube.com/feeds/videos.xml?channel_id=UCRGn72Qu0KTtI_ujNxRr3Fg';
$xml = simplexml_load_file($url);
$ns = $xml->getDocNamespaces(true);
$xml->registerXPathNamespace('a', 'http://www.w3.org/2005/Atom');
$elements = $xml->xpath('//a:entry');
$content = $elements[0];
$yt = $content->children('http://www.youtube.com/xml/schemas/2015');
$media = $content->children('http://search.yahoo.com/mrss/');
echo "Video ID: " . $yt->videoId . "<br>";
echo "Video title: " . $content->title . "<br>";
echo "Upload date: " . $content->published . "<br>";
echo "Description: " .$media->group->description . "<br>";
If you want to display the information from all the entries, you can use:
foreach ($elements as $content) {
$yt = $content->children('http://www.youtube.com/xml/schemas/2015');
$media = $content->children('http://search.yahoo.com/mrss/');
echo "Video ID: " . $yt->videoId . "<br>";
echo "Video title: " . $content->title . "<br>";
echo "Upload date: " . $content->published . "<br>";
echo "Description: " . $media->group->description . "<br>";
echo "<br>";
}

Get latest Tweets - What API

I know this has been discussed a lot here, but I still don't seem able to find aworking solution. Either it's out of date, it's the wrong programming-language or it's no longer supported.
All I want to do is: Get the last 'n' tweets from a public Twitter profile using PHP/JavaScript.
What API should I use best?
How do I keep it as lightweight as possible?
I can't use Node. js
I tried this, but I can't seem to get it to work, as simple as it may look like.
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
I've already registered for a developer account and created a "Twitter-App".
You can use this API, which I have used once and it works perfectly.
Download the file TwitterApiExchange.php from the repository above and you may use the following sample code to fetch tweets of a user:
<?php
echo '<pre>';
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$user = "rootcss";
$count = 100;
if (isset($_GET['user'])) {
$user = $_GET['user'];
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
}
$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), $assoc = TRUE);
if (isset($string["errors"][0]["message"])) {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>" . $string['errors'][0]["message"] . "</em></p>";
exit();
}
foreach ($string as $items) {
echo "Time and Date of Tweet: " . $items['created_at'] . "<br />";
echo "Tweet: " . $items['text'] . "<br />";
echo "Tweeted by: " . $items['user']['name'] . "<br />";
echo "Screen name: " . $items['user']['screen_name'] . "<br />";
echo "Followers: " . $items['user']['followers_count'] . "<br />";
echo "Friends: " . $items['user']['friends_count'] . "<br />";
echo "Listed: " . $items['user']['listed_count'] . "<br /><hr />";
}
echo '</pre>';
?>
It worked perfectly for me, Let me know if you face any issues.

Not getting deal id in Pipe drive with push notification api?

Not getting deal id when the stage of deal is changed in Pipe drive with push notification api. I am getting the response
echo $result = file_get_contents('https://api.pipedrive.com /v1/pushNotifications?api_token=my-appid');
This is the code I have inserted when the http post is run
{"success":true,"data":
[{"id":7,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"updated.deal","http_auth_user":"","http_auth_password":"",
"add_time":"2015-08-03 07:41:04","active_flag":true,"remove_time":null,"http_last_response_code":200},
{"id":8,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"*.deal","http_auth_user":"","http_auth_password":"","add_time":"2015-08-03 08:47:56",
"active_flag":true,"remove_time":null,"http_last_response_code":200}],"additional_data":{"pagination":
{"start":0,"limit":100,"more_items_in_collection":false}}
}
Please let me know How I can get the deal which stage has been changed?
You can use json_decode function.
Try example
$result = file_get_contents('https://api.pipedrive.com/v1/pushNotifications?api_token=my-appid');
$result = json_decode($result, true);
foreach($result['data'] as $deal)
{
echo "ID: ".$deal['id']."<br />";
echo "User ID: ".$deal['user_id']."<br />";
echo "Company ID: ".$deal['company_id']."<hr />";
}
UPDATE
$result = <<<JSON
{"success":true,"data":
[{"id":7,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"updated.deal","http_auth_user":"","http_auth_password":"",
"add_time":"2015-08-03 07:41:04","active_flag":true,"remove_time":null,"http_last_response_code":200},
{"id":8,"user_id":750195,"company_id":538560,"subscription_url":"https:\/\/empathi-solutions.com\/pipe\/pip.php",
"type":"regular","event":"*.deal","http_auth_user":"","http_auth_password":"","add_time":"2015-08-03 08:47:56",
"active_flag":true,"remove_time":null,"http_last_response_code":200}],"additional_data":{"pagination":
{"start":0,"limit":100,"more_items_in_collection":false}}
}
JSON;
$result = json_decode($result, true);
foreach($result['data'] as $deal)
{
echo "ID: ".$deal['id']."<br />";
echo "User ID: ".$deal['user_id']."<br />";
echo "Company ID: ".$deal['company_id']."<hr />";
}

Illegal offset with simple mysqli read query

This is driving me crazy. The page I'm trying to create is to read from a mysql DB and then create a JSON object to return to an Angular app. Please bear with the inserted code blocks, it's all pertinent.
The overall question is this: Why am I getting the offset warnings for 'definition' and 'type' from the $term array, but not the others?
$definition = $term['definition']; (line 31)
$type = $term['type']; (line 33)
I did notice that each time it loops through the array, the values it outputs for both definition and type are the first letter of the term. So if the term is "Third Party Charges", then the definition and type both still get warnings but are assigned the value equal to left($term, 1).
Since there is a lot down there, specifically these two lines cause the warnings:
But these work fine:
$caseName = $term['caseName'];
$caseNumber = $term['caseNumber'];
$term = $term['term'];
Thanks for all help. I'm sure it being so late/early has something to do with this not making sense to me.
Here is the PHP:
$mysqli = new mysqli("localhost", "user", "pw", "db")
or die('Error connecting to the db.');
//Check for connection to database
if ($mysqli->errno) {
printf("Unable to conect to the database: %s" ,$mysqli->error);
exit();
}
$query = "Select dd.caseName, dd.caseNumber, dd.term, dd.definition, dd.type
From `ddMain` dd";
$termlist = $mysqli->query($query, MYSQLI_STORE_RESULT)
or die("Uh oh.");
while($term = $termlist->fetch_array(MYSQL_ASSOC)) {
echo "<pre>";
var_dump($term);
echo "</pre>";
$caseName = $term['caseName'];
echo "CaseName: " . $caseName . "<br />";
$caseNumber = $term['caseNumber'];
echo "CaseNumber: " . $caseNumber . "<br />";
$term = $term['term'];
echo "Term: " . $term . "<br />";
$definition = $term['definition'];
echo "Definition: " . $definition . "<br />";
$type = $term['type'];
echo "Type: " . $type . "<br />";
printf("Nm: %s, #: %s, term: %s, def: %s, type: %s", $caseName, $caseNumber, $term, $definition, $type);
}
Here is the output from the var_dump of $term:
array(5) {
["caseName"]=>
string(10) "Case name here"
["caseNumber"]=>
string(6) "123456"
["term"]=>
string(13) "Doughnut Hole"
["definition"]=>
string(168) "Some text here."
["type"]=>
string(8) "Business"
}
And finally the output of the loop up there:
CaseName: Case name here
CaseNumber: 123456
Term: Doughnut Hole
Warning: Illegal string offset 'definition' in C:\xampp\htdocs\path\file.php on line 31
Definition: D
Warning: Illegal string offset 'type' in C:\xampp\htdocs\path\file.php on line 33
Type: D
Nm: Case name here, #: 123456, term: Doughnut Hole, def: D, type: D
$term = $term['term'];
It's because that particular line is changing $term to something other than the database row. You're using $term for both the returned array from the database and one of the values extracted from it. Use a different name for one of them and your problem will be solved.
One possibility is to change:
$term = $term['term'];
echo "Term: " . $term . "<br />";
to something like:
$term2 = $term['term'];
echo "Term: " . $term2 . "<br />";

How to get a value from wunderground .json

I'm working on json source from wunderground.com. As the example code displayed in the document. I can adjust and work out for some simple format. But I'm stuck with this one. I tried to googled every where but there's no solution.
Here's the sample codes:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
?>
Well, I need some information like "Cedar Rapids" out of pws/station :
"pws": {
"station": [
{
"neighborhood":"Ellis Park Time Check",
"city":"Cedar Rapids",
"state":"IA",
"country":"US",
"id":"KIACEDAR22",
"lat":41.981174,
"lon":-91.682632,
"distance_km":2,
"distance_mi":1
}
]
}
(You can get all code by clicking this : http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json )
Now the questions are:
What is this data called? (array, array in array?)
How could I pull this data out of the line?
Regards,
station is an array within the pws object.
To get the data, you can do something like this:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/b8e924a8f008b81e/geolookup/conditions/q/IA/Cedar_Rapids.json");
$parsed_json = json_decode($json_string);
$location = $parsed_json->{'location'}->{'city'};
$temp_f = $parsed_json->{'current_observation'}->{'temp_f'};
echo "Current temperature in ${location} is: ${temp_f}\n";
$stations = $parsed_json->{'location'}->{'nearby_weather_stations'}->{'pws'}->{'station'};
$count = count($stations);
for($i = 0; $i < $count; $i++)
{
$station = $stations[$i];
if (strcmp($station->{'id'}, "KIACEDAR22") == 0)
{
echo "Neighborhood: " . $station->{'neighborhood'} . "\n";
echo "City: " . $station->{'city'} . "\n";
echo "State: " . $station->{'state'} . "\n";
echo "Latitude: " . $station->{'lat'} . "\n";
echo "Longitude: " . $station->{'lon'} . "\n";
break;
}
}
?>
Output:
Current temperature in Cedar Rapids is: 38.5
Neighborhood: Ellis Park Time Check
City: Cedar Rapids
State: IA
Latitude: 41.981174
Longitude: -91.682632

Categories