mlla2 Posted February 15, 2009 Share Posted February 15, 2009 *NOTE* I'm not using this as a spam tool, it's just a practice. -- OK, it's fairly simple to ask Twitter for everyone following user X, but the hard part is (for me)... it sends it back the information as XML (there's a json option but that just seems more confusing). EX: <?xml version="1.0" encoding="UTF-8"?><br /> <users type="array"><br /> <user><br /> <id>1234</id><br /> <name>name</name><br /> <screen_name>screen name</screen_name><br /> <location>location</location><br /> <description>I am...blahblahblah..</description><br /> <profile_image_url>http://imgurl.ext</profile_image_url><br /> <url>http://mysite.com</url><br /> <protected>false</protected><br /> <followers_count>1234</followers_count><br /> <status><br /> <created_at>Sun Feb 15 19:46:38 +0000 2009</created_at><br /> <id>1234</id><br /> <text>Tweet here... the latest</text><br /> <source>where the tweet was from</source><br /> <truncated>false</truncated><br /> <in_reply_to_status_id></in_reply_to_status_id><br /> <in_reply_to_user_id></in_reply_to_user_id><br /> <favorited>false</favorited><br /> <in_reply_to_screen_name></in_reply_to_screen_name><br /> </status><br /> </user> But of course, it repeats that for every person. I'm trying to get the data between the <screen_name> tags, and ONLY echo that. i.e., "screen_Name_A screen_name_B [...]" But I'm either using the wrong function or reg exp. I think it's my reg exps... preg_match("/\<screen_name\>(.*)\</screen_name\>/", $response, $out); echo $out[0]; $response being what Twitter sends back, shown up ^ there. I've tried using a foreach w/ echo'ing $out[0][0], [1][0], [0][1], [1], only $out, ETC. Everything I could find anywhere... I don't know how a two-dimensional array would come into play but that's what the php.net doc gave as an example. ANY ideas? I've literally been toying with this for hours... sorry if it's not my regexp that's messing up, sorry if it's been posted somewhere, and sorry once again if I didn't explain everything correctly. *exits with /faildance* Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 15, 2009 Share Posted February 15, 2009 Hmm.. would simpleXML would work in this case? Here could be one preg solution: preg_match_all('#<screen_name>([^<]+)</screen_name>#', $str, $matches); // $str represents you chunk of code... echo "<pre>".print_r($matches[0], true); Quote Link to comment Share on other sites More sharing options...
mlla2 Posted February 15, 2009 Author Share Posted February 15, 2009 I thought about that, but I wasn't sure it'd be installed (I'm using a shared hosting service, 1&1) so I didn't want to sit around figuring it out with that risk (everything seems more complex on php.net...), but I'll try it now. Result: 'Array ( )' I didn't think this would matter, but... the XML is parsed by the browser or whatever, so it doesn't show any tags except in the source code... would that affect what I need to do with regexps? Here, I'll show you the live script: http://ifueling.com/insertTwitterMsg.php Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 15, 2009 Share Posted February 15, 2009 I've never tried regex on XML to be perfectly honest. and I have no idea how Twitter works.. Could the XML file in question be passed into a variable via file_get_contents and have that checked through preg? Quote Link to comment Share on other sites More sharing options...
mlla2 Posted February 15, 2009 Author Share Posted February 15, 2009 I doubt it. I'm using this function... function httpRequest($host, $path = '/', $method = 'GET') { global $errno, $errstr, $response; global $twitter_username, $twitter_password; $header = "$method $path HTTP/1.1\r\n"; $header .= "Host: $host\r\n"; $header .= "Accept-Encoding: none\r\n"; $header .= "Authorization: Basic " . base64_encode("{$twitter_username}:{$twitter_password}") . "\r\n"; $header .= "Connection: Close\r\n\r\n"; $sock = fsockopen($host, 80, $errno, $errstr, 30); if (!$sock) { die("<p><strong>fsockopen() error:</strong><br />$errstr ($errno)</p>"); } else { fwrite($sock, $header); while (!feof($sock)) { $response .= fgets($sock, 128); } fclose($sock); /*$response = trim(str_replace(array('<', '>'), array('<', '>'), $response));*/ return true; } } To get that information, via; httpRequest('twitter.com', '/statuses/followers/'.$who.'.xml', 'GET'); Because that's what the Twitter API Wiki says to do (apiwiki.twitter.com): followers Returns the authenticating user's followers, each with current status inline. They are ordered by the order in which they joined Twitter (this is going to be changed). URL: http://twitter.com/statuses/followers.format Formats: xml, json Method(s): GET Parameters: * id. Optional. The ID or screen name of the user for whom to request a list of followers. Ex: http://twitter.com/statuses/followers/12345.json or http://twitter.com/statuses/followers/bob.xml * page. Optional. Retrieves the next 100 followers. Ex: http://twitter.com/statuses/followers.xml?page=2 Returns: list of basic user information elements EDIT: I could save the data to file and then get that via file_get_contents() couldn't I? Quote Link to comment Share on other sites More sharing options...
.josh Posted February 15, 2009 Share Posted February 15, 2009 nrg_alpha's code works fine on the example data you posted. You said that the data is being stored into $response. Did you change $str to $response in nrg_alpha's code? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted February 15, 2009 Share Posted February 15, 2009 In a small mini test, I created an XML file in the root of my site (the XML info is what you have initially posted, but I doubled the entries and changed the content within <screen_name> to fluffy and Cornflakes respectively, and managed to extract this info as such: $str = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/newXMLDocument.xml'); preg_match_all('#<screen_name>([^<]+)</screen_name>#', $str, $matches); // $str represents you chunk of code... echo "<pre>".print_r($matches[0], true); Output: Array ( [0] => fluffy [1] => Cornflakes ) but as far as the Twitter API is concerned, I never used it. If the twitter site doesn't give enough explanation in how to do it, perhaps Googling your issue? Sorry I can't really be of any further assistance. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.