Link to home
Start Free TrialLog in
Avatar of Nathan Riley
Nathan RileyFlag for United States of America

asked on

How to pull out data from array?

I'm trying to pull data out to insert into database from the following link:
http://bit.ly/PBIgJI

How can I call different fields like "text" and other elements?
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

That looks like the output of a var_dump command.

If that's the case, you aren't supposed to import that.
Use serialize or, better, json_encode.

HTH,
Dan
Should be fairly simple
$input = 'http://bit.ly/PBIgJI';
$input = file_get_contents($input);
parse_str($input, $output);

Open in new window

$output should contain the parsed info.
Belay that - might not work 100% ... working on another approach.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
trying to pull data out
You'll get a more specific answer if you do these two things.

1. Tell us what data you want.  Each of the 15 Tweets in this stream contains some data elements that are useful and probably some that are not.  Give us the names of the fields you would like to extract.

2. Give us access to the original data, not a var_dump() representation of the data.  Var_Dump() output is useful for visualization and debugging, but we need the original data if we are going to show you the correct programmatic way to access the information in the Tweets.  

The original was probably a JSON string (because that's what everybody uses for API output today) but it might have been an XML document.  In either case, there is a PHP function for dealing with the original data.  A programmer would use SimpleXML_Load_String() or JSON_Decode().  Either of those functions will make an object that can be iterated with foreach().
Avatar of Nathan Riley

ASKER

@julian - can I use this against the data that's pulled via a php file instead of .txt?

@Ray - Sure thing the line used to pull the data:
$result = $twitter->search_tweets('q=#'.$q.'&include_entities='.false.'&result_type=recent', false);

Open in new window


It's part of codebird library to pull twitter tweets.  The reason I'm trying to modify is the default setup doesn't seem to pull in tweets that I'm searching for via hashtags.  Here is my code as well if that helps:

<?php
require_once __DIR__.'/../includes.php';

/* TWITTER OBJECT */
\Codebird\Codebird::setConsumerKey(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET); // static, see 'Using multiple Codebird instances'

$twitter = \Codebird\Codebird::getInstance();

$twitter->setToken(TWITTER_ACCESS_TOKEN, TWITTER_TOKEN_SECRET);

/* QUERY -- used in facebook, twitter, and instagram searching */
$q = 'americancrew';


/*  GET TWITTER POSTS */

$result = $twitter->search_tweets('q=#'.$q.'&include_entities='.false.'&result_type=recent', false);
//$result = $twitter->statuses_userTimeline('screen_name='.urlencode(TWITTER_SCREEN_NAME));

//var_dump($result);

if (isset($result)) {
	
	$statuses = (array)$result;
		
	if (is_array($statuses)) {
	
		foreach ($statuses as $tweet) {
			
			if (!empty($tweet->text)) {
				
				$text = $tweet->text;
				
				
				$added_chars = 0;
				
				if (!empty($tweet->entities->media) && is_array($tweet->entities->media)) {
					
					foreach ($tweet->entities->media as $media) {
						
						if (!empty($media->url)) {
						
							$url = '<a href="'.$media->url.'" target="_blank">'.$media->url.'</a>';
							
							$text = str_replace($url, $media->url, $text);							
							$text = str_replace($media->url, $url, $text);	
						}
					}	
				}	
				
				if (!empty($tweet->entities->urls) && is_array($tweet->entities->urls)) {
					
					foreach ($tweet->entities->urls as $url_obj) {
						
						if (!empty($url_obj->url)) {
						
							$url = '<a href="'.$url_obj->url.'" target="_blank">'.$url_obj->url.'</a>';
					
							$text = str_replace($url, $url_obj->url, $text);						
							$text = str_replace($url_obj->url, $url, $text);	
	
						}
					}	
				}
				
				if (!empty($tweet->entities->user_mentions) && is_array($tweet->entities->user_mentions)) {
					
					foreach ($tweet->entities->user_mentions as $user) {
						
						if (!empty($user->screen_name)) {
						
							$url = '<a href="http://twitter.com/'.$user->screen_name.'" target="_blank">@'.$user->screen_name.'</a>';
							
							$text = str_replace('@'.$user->screen_name, $url, $text);	
		
						}
					}	
				}
				
				if (!empty($tweet->entities->hashtags) && is_array($tweet->entities->hashtags)) {
					
					foreach ($tweet->entities->hashtags as $hashtag) {
						
						if (!empty($hashtag->text)) {
						
							$url = '<a href="http://twitter.com/search?q=%23'.$hashtag->text.'" target="_blank">#'.$hashtag->text.'</a>';
							
							$text = str_replace('#'.$hashtag->text, $url, $text);	
		
						}
					}	
				}				
						
				
				$insert = array();
				$insert['source'] = SOURCE_TWITTER;
				$insert['source_id'] = SOURCE_TWITTER.'_'.$tweet->id_str;
				$insert['text'] = $text;
				$insert['type'] = 'text';
				//$insert['media_thumbnail_url'];
				//$insert['media_url'];
				$insert['author'] = $tweet->user->name;
	
				if (!empty($tweet->user->screen_name)) {
					
					$insert['author_url'] = 'http://twitter.com/'.$tweet->user->screen_name;
				}
	
				
				if (is_numeric($tweet->created_at)) {
					$post_date = intval($tweet->created_at);	
				} else {
					$post_date = strtotime($tweet->created_at);	
				}
				
				$insert['post_date'] = date("Y-m-d H:i:s", $post_date);			
				$insert['date_added'] = date("Y-m-d H:i:s");
				$insert['date_updated'] = $insert['date_added'];
	
				$db->insert('posts', $insert);				
				
				
			}			
			
		}
		
	}
	
}

Open in new window

So the thing we were looking at here (http://bit.ly/PBIgJI) was the output of var_dump($result); right?

In order to access the twitter feed, we would need the information in the included files, including the Codebird stuff.

Without access to the Twitter feed, it is impossible to test a solution, but we may be able to get you some examples.
Yes correct.  I tried echoing out, but just received an error, I assume b/c it's an array.
can I use this against the data that's pulled via a php file instead of .txt?

Yes - if your ISP supports it you can simply change the

define('SOURCE','vardump.txt');

To

define('SOURCE','http://bit.ly/PBIgJI');

Some ISP's disable the ability to use file_get_contents to load remote pages - if so you would then need to look at the cURL library to pull the data.

Either way - all that is required is to load the file into the $input variable in the get_line function.
My guess is that it's an object, not an array.  What did you see when you tried echo?
Also, what is the definition of this method?  What does the FALSE argument mean?  Have you tried constructing the input argument separately (not loading it into the function call) so you can see what is being sent to the method?

$result = $twitter->search_tweets('q=#'.$q.'&include_entities='.false.'&result_type=recent', false);
@Ray - Catchable fatal error: Object of class stdClass could not be converted to string in /home/convo/public_html/americancrew/cron/pull-twitter1.php on line 19
I'm not familiar with the code.  I'm not sure what false/true means in that statement and unsure where to go to find how it works.  I assume it has something to do with codebird?  I can attach the source file.
codebird.php
OK, that's useful information.  It means that $result should be iteratable in its existing state.  In other words, the API call has already converted the JSON or XML string into a PHP object.
@julian that appears to work without errors, here is the output:
http://conversationdevelopment.com/americancrew/cron/nathan.php

I'm wondering now in that page after you functions how I would call each field like:
created_at
id
text
source
user
url
entities

Take those and get them ready to insert into my database.  I know you said you saw some nuances above is this a viable solution for this or is there a better solution?
Once you run that code the data is now in a standard PHP structure - an array of objects that contain the data.

The dump is actually of 3 objects so the returned array will contain each of these in postions 0 through 2.

To access the first we can do like so
$status = $object[0];

echgo $status['statuses'][0]->id'

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems to only pull the most recent 15.  Any ideas on where I could increase that limit?  I don't show anything limiting it in your code.
This was a good solution Ray, the only thing missing from the array in your solution is the "media_url" if a picture is posted it would pull that URL as well as shown in the var_dump, but not is your $sta array.