2010-11-05 61 views
0

我需要您卓越的Web开发洞察力。我正在使用Imageshack API,并设法使用从我的服务器到ImageHack服务器的表单上载图像,并且它返回了一个相当混乱的数据。解析从imageshack返回的数据API

现在我的问题是,我该怎么做才能得到我需要的东西。我必须解析它吗?我讨厌正则表达式(但如果必须的话,我会处理它)。如果我必须解析它,我将如何将整个字符串发送到一个函数?我有点困惑。

<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> 

我回来的数据看起来像这样。

http://img574.imageshack.us/img574/3084/18835698.png

我想我真正的问题是,当用户按下提交按钮,它给他的垃圾,我怎么分析它动态,并给他一个漂亮的结果。

+1

查看页面源,并添加另一截屏的,我们需要看到的数据是什么格式 – RobertPitt 2010-11-05 11:46:04

+0

@RobertPitt看起来根据他们已添加的标签是XML。对他们的问题。 – Treffynnon 2010-11-05 11:52:39

+0

然后研究XML Parser。 – RobertPitt 2010-11-05 11:55:58

回答

0

从你问题上的标签我将假设返回的数据是XML,并建议你看看本地PHP SimpleXML函数来解析响应。

PHP手册包含了应该让你去一个基本的例子:Basic usage

0

你想在PHP运行在做这个。

之后,你有你的XML,这里有一个小的功能,可用于获取信息:

/** 
* 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])); 
    } 
} 

的功能来源:http://www.sourcer.cz/ci-lib/imageshack/

用法是,像这样:

$xml = file_get_contents('http://www.imageshack.us/upload_api.php?url=http://www.mysite.com/myimage.png'); 
$image_link = getInfo($xml,'image_link'); 
+0

感谢您的快速回复家伙,这是一个问题,虽然....我怎么得到的数据来解析它。我猜我必须为此使用CodeIgniter Imageshack库。 – user498245 2010-11-05 12:39:24

+0

不,我从该库中提取了一种方法供您使用,您不需要CI,我只是引用了其他人的工作。 – RobertPitt 2010-11-05 12:41:56

0

啊,谢谢,我现在明白了。我做了一些进一步的研究,并另一种方式可能是使用curl,像这样:

使用cURL送岗位“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); 
+0

@罗伯特,你的方法绝对看起来更容易。 – user498245 2010-11-05 12:47:22

+0

嗯,好吧我现在得到它...我必须允许用户上传我的服务器中的文件,然后从url(基于我的服务器)生成XML。我希望绕过使用我的服务器来使事情更安全。我可以检查文件是否以.jpg,.png结尾以及不是。但是这仍然不能保证安全,是吗?可能是我可以用curl生成$ xml。然后使用getinfo()函数? – user498245 2010-11-05 12:57:08

1

你应该使用动态页面,允许您使用CURL库或更简单的Ajax函数。

例如,在使用PHP的CURL传递所有东西之后,可以使用simpleXML获取返回的XML页面的url形式。

0

所有你需要的是在这个环节:

http://code.google.com/p/imageshackapi/source/browse/RedirectAPI.wiki?repo=wiki

你应该把这种形式

<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"/> 

得到图像的URL:

$img_url="http://img$var1.imageshack.us/img$var1/$var2/$var3"; 

得到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"; 
+0

你能提供一个你的链接的上下文吗? – 2012-02-16 13:32:59