0

我需要您出色的 Web 开发洞察力。我正在使用 Imageshack API,并设法使用表单将图像从我的服务器上传到 imageshack 服务器,它返回给我一个相当混乱的数据。

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

<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

我想我的问题确实是,每当用户按下提交按钮时,它都会给他垃圾,我如何动态解析它,并给他一个漂亮的结果。

4

5 回答 5

1

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

例如,在使用 PHP 的 CURL 传递所有内容后,您可以使用 simpleXML 从返回的 XML 页面获取 url。

于 2011-02-12T10:47:16.273 回答
0

根据您问题的标签,我将假设返回的数据是 XML,并建议您查看原生 PHP SimpleXML函数来解析响应。

PHP 手册包含一个基本示例,可以帮助您入门:基本用法

于 2010-11-05T11:50:24.957 回答
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);
于 2010-11-05T12:44:50.197 回答
0

您想在 PHP Runtime 中执行此操作。

获得 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');
于 2010-11-05T12:03:34.947 回答
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"/>

获取图片网址:

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

获取缩略图:

$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";
于 2012-02-16T10:06:45.080 回答