Fun Coding Experiment: commandline PHP/JSON Parser

So, I have been coding like a mad person and I needed a simple JSON parser to get declared variables for PHP API Programming. So, This is what I came up with :-D

To run it, you will have to have php-cli enabled. The base install should come with cURL, but if it doesn’t then make sure to recompile :-P

$> php json.php http://api.twitter.com/1/statuses/user_timeline?screen_name=twitterapi

And the code is here:

<?php
	// Command Line JSON Parser.

	if(isset($argv[1])) {

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $argv[1]);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$ret = curl_exec($ch);

		if(curl_errno($ch)) {
			print "Error with CURL: ".curl_error($ch);
		}

		curl_close($ch);

		print_r(json_decode($ret, true));
	}
Posted by Duane on June 10th, 2010 9:37 pm | Tags: coding experiment, commandline, fun, JSON, PHP
Posted in Code | 1 Comment »
  • http://conigliaro.org/ Michael Conigliaro

    Hi Duane. I solved the same problem in Ruby and packaged it as a Gem. You can even use it to query the resulting JSON object for specific values you’re interested in. Check it out:

    https://github.com/mconigliaro/jazor

About Me
Me

Duane Jeffers

  • Game Designer
  • Web Developer
 
June 2010
M T W T F S S
« May   Sep »
 123456
78910111213
14151617181920
21222324252627
282930  
What I'm Doing...
  • And with a quiet button press in the middle of the night, version 2.1 of my project went live. Very excited that it went smoothly. 6 days ago
  • Congratulations @TimOfLegend ... 1 million dollars on Kickstarter! 1 week ago
  • @cuppy I agree. My heart skipped when I saw that. Also, I thought EA had the license to Monopoly. O.o Something smells ... off 1 week ago
  • @Keilantra THAT. SOUNDS. AWESOME!!!! :-D (but yeah, if you need a linux guy, I can fulfill that :P) 1 week ago
  • First year that I've ever really had to buy V-Day gifts. O.O #fb 1 week ago
  • @Keilantra ... setup IPTables to block all the ports or even not allow access from other IPs than a certain one. :-D 1 week ago
  • @Keilantra Lovely ... I can't tell if iRobot OS is linux based or what. I'm going to assume that it is ... and what you should do is setup 1 week ago
  • @Keilantra Depends ... what operating system does it run? :-P 1 week ago
  • @TimOfLegend Congrats! But! I also want to issue you a challenge. If your kickstarter makes 2 million bucks, I'll let you name my first born 1 week ago
  • Win a $5,000 Golden Parachute to pay off your Student Loans! Enter to get bailed out here: http://t.co/sUk219Ty via @skillshare 1 week ago
  • More updates...