2010-11-05 51 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