Parsing data returned from imageshack API
I am in need of your superior web development insight. I was playing with the Imageshack API, and man开发者_StackOverflowaged to upload a image using a form from my server to imageshack server, and it returns me a rather confusing data back.
Now my question is, what the hell do I do with that to get what I need. Do I have to parse it? I hate regular expressions (but I will deal with it, if I have to). If I have to parse it, how would I send the entire string to a function? I am a bit confused.
<form method="post" enctype="multipart/form-data" action="http://www.imageshack.us/upload_api.php">
<p><input type="file" name="fileupload"></p>
<p><input type="text" name="tags" value="proba,test"></p>
<p><input type="text" name="key" value="xxxxxxxxxxxxxxxxxxxxxxxxxx"></p>
<p><select name="optsize">
<option value="320x240">Small (320x240)</option>
<option value="426x320" selected>Medium (426x320)</option>
<option value="640x480">Big (640x480)</option>
</select></p>
<p><input type="submit" value="Go"></p>
</form>
The data I get back looks something like this.
http://img574.imageshack.us/img574/3084/18835698.png
I guess my question really is, whenever a user presses a submit button, it gives him that junk, how do I parse it dynamically, and give him a pretty result.
You should use a dynamic page that allows you to use the CURL lib or more simply an Ajax function.
For example, after passing all the stuff using PHP's CURL you can get the url form the returned XML page using simpleXML.
From the tags on your question I am going to assume the returned data is XML and recommend you look into the native PHP SimpleXML functions to parse the response.
The PHP manual contains a basic example that should get you going: Basic usage
You want to be doing this in PHP Runtime.
After you have your xml, here's a small function that can be used to get the info:
/**
* parsing XML response for info about uploaded file
* image_link - URL address of uploaded image on imageshack server
* thumb_link - URL address of thumbnail of uploaded image on imageshack server
* yfrog_link - URL address of uploaded image on yfrog server
* yfrog_thumb - URL address of thumbnail of uploaded image on yfrog server
* ad_link - URL address of imgaeshack page with uploaded image
* done_page - URL address of thumbnail of uploaded image on imageshack server
* width - width of uploaded image [px]
* height - height of uploaded image [px]
*/
function getInfo($xml,$key)
{
preg_match('/<'.$key.'>(.*)<\/'.$key.'>/', $xml, $value);
if (empty($value[0])) {
return('invalid key');
}else {
return(strip_tags($value[0]));
}
}
Source of function: http://www.sourcer.cz/ci-lib/imageshack/
usage would be like so:
$xml = file_get_contents('http://www.imageshack.us/upload_api.php?url=http://www.mysite.com/myimage.png');
$image_link = getInfo($xml,'image_link');
Ah, Thanks I get it now. I did some further research and, another way might be using curl like so :
Using cURL to sent post to "http://www.imageshack.us/upload_api.php"
$data['key'] = API_KEY;
$data['public'] = "yes";
$data['xml'] = "yes";
$data['fileupload'] = '@'.$dest;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.imageshack.us/upload_api.php');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 600);
/*curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);*/
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
all you need is in this link:
http://code.google.com/p/imageshackapi/source/browse/RedirectAPI.wiki?repo=wiki
You should put this in the form
<input type="hidden" name="success_url" value="mysite.com/success.php?var1=%s&var2=%b&var3=%i">;
<input type="hidden" name="error_url" value="error.php">
<input type="hidden" name="optsize" id="optsize" value="200x380"/>
<input type="hidden" name="optimage" id="optimage" value="1"/>
<input type="hidden" name="rembar" id="rembar" value="1"/>
to get the image url:
$img_url="http://img$var1.imageshack.us/img$var1/$var2/$var3";
to get the tumbnail:
$var3= str_replace (".jpg", ".th.jpg", $var3);
$var3= str_replace (".gif", ".th.gif", $var3);
$var3= str_replace (".png", ".th.png", $var3);
$tumb_url="http://img$var1.imageshack.us/img$var1/$var2/$var3";
精彩评论