new here!!
Im trying to get tweets to display on a site(framework is codeigniter). I am using the twitter api (for example: https://api.twitter.com/1/statuses/user_timeline/ddarrko.xml) to get the tweets and subsequently insert them into a database which I will then get to display on the site. The actual code runs fine the issue is it only ever processes one tweet. my code is -
//get twitter address
$this->load->model('admin_model');
$getadd = $this->admin_model->get_settings("twitter_address");
$twitter_user = $getadd->item_value;
//define twitter xml file
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
foreach ($xml->status as $tweet);
{
echo "<pre>";print_r($xml);echo "</pre>";
$this->data->username=$twitter_user;
$this->data->twitter_status=$tweet->text;
$this->data->pub_date=$tweet->created_at;
//load model and insert tweets;
$this->load->model('tweet_model');
$this->tweet_model->insert_tweets($this->data);
}
as you can see I am defining to run each status in xml file. The echo pre line is me testing because when printing $tweet only one tweet is coming up however even if i loop through just $xml still only one tweet is processed despite there being loads in the file.
any help/advice would be greatly appreciated!
Below code will give you all the available tweets from XML. you can manipulate below logic as per your need.
$xmlpath = "https://api.twitter.com/1/statuses/user_timeline/".$twitter_user.".xml";
$xml = simplexml_load_file($xmlpath);
$count_tweet = sizeof($xml);
for($i=0 ; $i < $count_tweet ; $i++)
{
echo"<br>Tweet: ". $xml->status[$i]->text;
echo"<br>date:".$xml->status[$i]->created_at."<br>";
}
Thanks.
Related
I am attempting to only run a loop if xml results actually exist. I am getting the xml results via:
$albums = simplexml_load_string(curl_get($api_url . '/videos.xml'));
What I want to be able to do is that on the next line say:
if($albums = hasAValue())
// Loop
Any ideas? Or a way to check before I load the XML data?
Side note: This is using the Vimeo API.
No, you need to further go down with the resultant with the namespace, reach till body give the xpath and work on.
$albums->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
To be specific, let me know the XML response you are getting i will let you the output.
UPDATED
$albums = simplexml_load_string("#your response#");
echo count($xml->children());
The dirty way:
$albums = #simplexml_load_string(curl_get($api_url . '/videos.xml'));
if ($albums)
{
...
}
This is dirty because of the Error Control Operator # which is used to "deal" with the error cases (e.g. problem fetching the remote location).
The alternative is to differentiate more here:
$xml = curl_get($api_url . '/videos.xml');
$albums = NULL;
if ($xml)
{
$albums = simplexml_load_string($xml);
}
if ($albums)
{
...
}
This script isn't working so good.
<?php
//include twitter class
require('classes/twitter.php');
$twitter = new Twitter('custToken', 'custSecret');
// set tokens
$twitter->setOAuthToken('blah blah');
$twitter->setOAuthTokenSecret('blah blah');
//$tweet = "This tweet was posted from a custom php script.";
//$twitter->statusesUpdate($tweet); // <-- this works!
$friends = $twitter->friendsList('tynamite');
foreach ($friends as $friend){
print_r($friend);
echo "<hr>";
}
?>
I want to display all my Twitter friends (everyone I follow) in a list.
The result I am getting is this.
Could anyone please help on this?
You should pass a cursor. You could use something like the code below (untested and just pseudo-code):
while($count == 20) {
$friends = $twitter->friendsList('tynamite', $cursor);
$count = count($friends);
$cursor++;
.. your code ..
}
twitter uses json to return its results. you need to parse the results returned and pull out the information you need. check this blog out for an example of how to use php to parse json
I'm fairly new to php although I've been programming for a couple years.
I'm working on a project and the end goal is to load certain elements of an xml file into an oracle table on a nightly basis. I have a script which runs nightly and saves a the file on my local machine. I've searched endlessly for answers but have been unsuccessful.
Here is an aggregated example of the xml file.
<?xml version="1.0" encoding="UTF-8" ?>
<Report account="7869" start_time="2012-02-23T00:00:00+00:00" end_time="2012-02-23T15:27:59+00:00" user="twilson" more_sessions="false">
<Session id="ID742247692" realTimeID="4306650378">
<Visitor id="5390643113837">
<ip>128.XXX.XX.XX</ip>
<agent>MSIE 8.0</agent>
</Visitor>
</Session>
<Session id="ID742247695" realTimeID="4306650379">
<Visitor id="7110455516320">
<ip>173.XX.XX.XXX</ip>
<agent>Chrome 17.0.963.56</agent>
</Visitor>
</Session>
</Report>
One thing to note is that the xml file will contain several objects which I will need to load into my table and the above example would just be for two rows of data. I'm familiar with the whole process of connecting and loading data into oracle and have setup similar scripts which perform ETL of txt. and csv. files using php. Unfortunately for me in this case the data is stored in xml. The approach I've taken when loading a csv. file is to load the data into an array and proceed from there.
I'm pretty certain that I can use something similar and perhaps create variable for each or something similar but am not really too sure how to do that with an xml. file.
$xml = simplexml_load_file('C:/Dev/report.xml');
echo $xml->Report->Session->Visitor->agent;
In the above code i'm trying to just return the agent associated with each visitor. This returns an error 'Trying to get property of non-object in C:\PHP\chatTest.php on line 11'
The end result would be for me to load the data into a table similar to the example I provided would be to load two rows into my table which would look similar to below however I think I can handle that if i'm able to get the data into an array or something similar.
IP|AGENT
128.XXX.XX.XX MSIE 8.0
173.XX.XX.XXX Chrome 17.0.963.56
Any help would be greatly appreciated.
Revised Code:
$doc = new DOMDocument();
$doc->load( 'C:/Dev/report.xml' );
$sessions = $doc->getElementsByTagName( "Session" );
foreach( $sessions as $session )
{
$visitors = $session->getElementsByTagName( "Visitor" );
foreach( $visitors as $visitor )
$sessionid = $session->getAttribute( 'realTimeID' );
{
$ips = $visitor->getElementsByTagName( "ip" );
$ip = $ips->item(0)->nodeValue;
$agents = $visitor->getElementsByTagName( "agent" );
$agent = $ips->item(0)->nodeValue;
echo "$sessionid- $ip- $agent\n";
}}
?>
The -> operator in PHP means that you are trying to invoke a field or method on an object. Since Report is not a method within $xml, you are receiving the error that you are trying to invoke a property on a non-object.
You can try something like this (don't know if it works, didn't test it and haven't written PHP for a long time, but you can google it):
$doc = new DOMDocument();
$doc->loadXML($content);
foreach ($doc->getElementsByTagName('Session') as $node)
{
$agent = $node->getElementsByTagName('Visitor')->item(0)->getElementsByTagName('agent')->item(0)->nodeValue;
}
edit:
Adding stuff to an array in PHP is easy as this:
$arr = array();
$arr[] = "some data";
$arr[] = "some more data";
The PHP arrays should be seen as a list, since they can be resized on the fly.
I was able to figure this out using simplexml_load_file rather than the DOM approach. Although DOM works after modifying the Leon's suggestion the approach below is what I would suggest.
$xml_object = simplexml_load_file('C:/Dev/report.xml');
foreach($xml_object->Session as $session) {
foreach($session->Visitor as $visitor) {
$ip = $visitor->ip;
$agent = $visitor->agent;
}
echo $ip.','.$agent."\n";
}
I am trying to retrieve data from this XML style file :
<Product_Group>
<Product_Group_ID>131</Product_Group_ID>
<Product_Group_Title>Thanks for the Memories</Product_Group_Title>
<Products>
<On_Sale_Date>03/01/12 00:00:00.000</On_Sale_Date>
<ISBN>9780007233694</ISBN>
<Title>Thanks for the Memories</Title>
<Format>Paperback</Format>
<Sub_Format/>
<CoverImageURL_Small>http://www.harpercollins.com/harperimages/isbn/small/4/9780007233694.jpg</CoverImageURL_Small>
</Products>
</Product_Group>
I am using following code but this seems to retrieve nothing. any help in fixing this issues would be highly appreciated
$xml = simplexml_load_string($response);
//$xml= $response;
$updates = array();
//loop through all the entry(s) in the feed
for ($i=0; $i<count($xml->Product_Group); $i++)
{
//get the id from entry
$ISBN = $xml->entry[$i]->ISBN;
//get the account link
$Title = $xml->entry[$i]->Title;
//get the tweet
$Product_Group_SEO_Copy = $xml->entry[$i]->Product_Group_SEO_Copy;
}
1) It is not valid XML. What warnings do you see? You'll need to fix them for simplexml_load_string to work properly.
For example, </CoverImageURL_Small> should be <CoverImageURL_Small/>
2) Assuming that Product_Group is not your actual document root (if it is than $xml points to it already and $xml->Product_Group will not work) then you can access each element like
$xml->Product_Group->Products[$i]->ISBN;
3) It's usually simpler to use a foreach loop than a for loop when dealing with simplexml
foreach($xml->Product_Group->Products as $p)
{
$ISBN = $p->ISBN;
//var_dump($ISBN);
}
hi i am useing zend Gdata Library for youtube videos i am trying to show more then 20 videos or i can set how much videos i want to show but i found no options
$yt = new Zend_Gdata_YouTube();
$videoFeed = $yt->getUserFavorites('liz');
is there a way to get more then 20 videos or less Zend Gdata default is 20
you can view here
http://framework.zend.com/manual/en/zend.gdata.youtube.html
After some more research I think I found some solution.
Check there http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Pagination
You should write recursive function loop trough video feed pages. For my app it was something like this (method in class):
<?php
//...
protected function build_favs_html($videos) {
//just saving html here. Mind the .= operator.
// I think you'll be doing this in some other way
$this->_html_response .= View::factory('videos')
->set('videos', $videos)
->set('type', 'load_favs');
// See whether we have another set of results
try {
$videos = $videos->getNextFeed();
}
catch (Zend_Gdata_App_Exception $e) {
//break function execution if there are no more result sets
return null;
}
//if there are result sets we continue calling same function on and on
$this->build_favs_html($videos);
}
I'm not sure if this can be done when requesting user favorites, as I've never used that feature, but when requesting videos via search terms you can set the results number via the setMaxResults method. You might be able to work this in with your user favorites request.
Here's a snippet of code we use:
$yt = new Zend_Gdata_YouTube();
$yt->setMajorProtocolVersion(2);
$query = $yt->newVideoQuery();
$query->setOrderBy('viewCount');
$query->setSafeSearch('strict');
$query->setFormat($searchFormat);
$query->setVideoQuery($searchTerms);
$query->setMaxResults($limit); // number of returned results set here
$query->setStartIndex($offset);
$results = $yt->getVideoFeed($query->getQueryUrl(2));