Main
Latest
- Teh First Law of Pwnage
- Lithium, Batteries & Recycling
- Muti.co.za & Censoring
- Milkshakes
- Breakfast: Yogurt, ja?
- Aphophysis
- South African Telecoms Industry Violates Hacker Et...
- More Joomla! Updates
- IT Salaries
- Pure Cheese & Calcium Chloride
Archives
- June 2004
- July 2004
- August 2004
- September 2004
- October 2004
- November 2004
- December 2004
- January 2005
- February 2005
- March 2005
- April 2005
- May 2005
- June 2005
- July 2005
- August 2005
- September 2005
- October 2005
- November 2005
- December 2005
- January 2006
- February 2006
- March 2006
- April 2006
- May 2006
- June 2006
- July 2006
- August 2006
- September 2006
- November 2006
- December 2006
- January 2007
- February 2007
- March 2007
- April 2007
- May 2007
- June 2007
- July 2007
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- May 2008
- June 2008
- July 2008
- August 2008
- September 2008
- October 2008
- November 2008
- December 2008
- January 2009
Tinylink API: Consumer PHP Shell Script
Here is a little script I wrote that consumes the Tinylink REST API.
#!/usr/bin/php
<?php
error_reporting(E_NONE);
$error = false;
foreach ($_SERVER['argv'] as $i => $uri) {
if ($i) {
$tinylink = file('http://tinylink.co.za/api/rest.php?url=' . urlencode($uri));
if ($tinylink) {
echo "$uri = $tinylink[0]\n";
} else {
echo "Error getting Tinylink for $uri\n";
$error = true;
}
}
}
if ($error) {
exit(1);
}
I can't figure out how to return a "1" to the calling process when there's an error. Any suggestions would be helpful.
I'm switching off error reporting so that the following output doesn't simply appear while you're piping the output from this script into some other script:
Warning: file(http://tinylink.co.za/api/rest.php?url=charlvn.za.net): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
in /home/charlvn/temp/tinylink.php on line 6
Oh yes, and you probably wanted usage instructions. Here are some examples:
- ./tinylink charlvn.za.net
- ./tinylink justinhartman.com stii.za.net charlvn.za.net
This assuming that you save the above file to somewhere as tinylink, that your CWD is the directory containing the script, and that you made the script executable (like with chmod +x tinylink). Oh yes and naturally you must have the PHP CLI installed and it's at /usr/bin/php (otherwise you'll just have to adjust the script as necessary).
This is a quick hack so not really a "proper" shell script but if you manage to come up with any improvements please comment and let us know! Thanks!
Update: Thanks to Peter Rotich and H* for helping me right seconds after posting. The answer was blatantly obvious: use exit(1)! Please see the updated script.
Copyright © 2004-2009 Charl van Niekerk. All articles are released under the Creative Commons Attribution 2.5 South Africa licence, unless where otherwise stated.


1 Comments
Comment by
H* on Tuesday, October 23, 2007 9:12:00 PM
exit(1); /* will do the right thing in PHP >= 4.2.0 (according to the docs) */
Post a Comment