PHPFlickr 脚本...这可以更干净/更精简吗?

发布于 2024-11-07 15:00:34 字数 1318 浏览 0 评论 0原文

好吧,这就是我的困境:

我读过很多人希望能够使用 PHPFlickr 显示来自 Flickr 的一组图像,但遗憾的是 PhotoSets 的 API 没有添加单独的照片描述。有些人尝试设置他们的 PHP,以便当脚本在页面上组装图库时,它会提取每张照片的描述。然而,该方法已经表明它是多么缓慢和低效。

我在其他地方发现了一个想法,即使用照片 ID 和描述创建一串逗号分隔值。我会将其存储在 MySQL 数据库中,然后当我让脚本在页面上组装图库时调用它。我会使用爆炸来创建照片 ID 及其描述的数组,然后调用该数组来填补空白......从而减少 API 调用并加快页面速度。

因此,在后端管理中,我有一个表单,用于设置图库信息,并提供一个设置 ID。然后,脚本将遍历并生成该字符串的分隔值(“|~|”作为分隔符)。这就是我的想法:

include("phpFlickr.php");
$f = new phpFlickr("< api >");

$descArray = "";

// This will create an Array of Photo ID from the Set ID.
// $setFeed is the set ID brought in from the form.
$photos = $f->photosets_getPhotos($setFeed);
foreach ($photos['photoset']['photo'] as $photo) {
    $returnDesc = array();
    $photoID = $photo['id'];
    $rsp = $f->photos_getInfo($photoID);
    foreach ($rsp as $pic) {
        $returnDesc[] = htmlspecialchars($pic['description'], ENT_QUOTES);
    }
    $descArray .= $photoID."|~|".$returnDesc[0]."|~|";
}

然后将字符串 $descArray 放入 MySQL 字符串中,将其与从表单中引入的其他信息一起放入数据库中。

我的第一个问题是我使用第二个 foreach 循环来获取这些描述是否正确?我尝试遵循网络上其他未使用该方法的示例,但它们从未起作用。当我启用第二个 foreach 时,它就起作用了。我应该做点别的事吗?

我注意到返回的数据将是两个条目。一个是描述,另一个只是一个“o”...因此数组 $returnDesc 这样我就可以得到我想要的一个字符串,而不是另一个。

第二个问题是我是否把这个弄得太复杂了。我喜欢尝试学习编写更干净/更精简的代码,并且正在寻找意见。

欢迎提出改进建议。先感谢您。

OK, here's my dilemma:

I've read all over about how many guys want to be able to display a set of images from Flickr using PHPFlickr, but lament on how the API for PhotoSets does not put individual photo descriptions. Some have tried to set up their PHP so it will pull the description on each photo as the script assembles the gallery on the page. However, the method has shown how slow and inefficient it can be.

I caught an idea elsewhere of creating a string of comma separated values with the photo ID and the description. I'd store it on the MySQL database and then call upon it when I have my script assemble the gallery on the page. I'd use explode to create an array of the photo ID and its description, then call on that to fill in the gaps...thus less API calls and a faster page.

So in the back-end admin, I have a form where I set up the information for the gallery, and I hand a Set ID. The script would then go through and make this string of separated values ("|~|" as a separation). Here's what I came up with:

include("phpFlickr.php");
$f = new phpFlickr("< api >");

$descArray = "";

// This will create an Array of Photo ID from the Set ID.
// $setFeed is the set ID brought in from the form.
$photos = $f->photosets_getPhotos($setFeed);
foreach ($photos['photoset']['photo'] as $photo) {
    $returnDesc = array();
    $photoID = $photo['id'];
    $rsp = $f->photos_getInfo($photoID);
    foreach ($rsp as $pic) {
        $returnDesc[] = htmlspecialchars($pic['description'], ENT_QUOTES);
    }
    $descArray .= $photoID."|~|".$returnDesc[0]."|~|";
}

The string $descArray would then be placed in the MySQL string that puts it into the database with other information brought in from the form.

My first question is was I correct in using a second foreach loop to get those descriptions? I tried following other examples all over the net that didn't use that, but they never worked. When I brought on the second foreach, then it worked. Should I have done something else?

I noticed the data returned would be two entries. One being the description, and the other just an "o"...hence the array $returnDesc so I could just get the one string I wanted and not the other.

Second question is if I made this too complicated or not. I like to try to learn to write cleaner/leaner code, and was looking for opinions.

Suggestions on improvement are welcome. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只想待在家 2024-11-14 15:00:34

我不是 100% 确定,因为我刚刚浏览了 phpFlickr,并查看了 Flickr API用于 getInfo() 调用。但无论如何让我尝试一下:)

首先,看起来你不应该需要那个循环,就像你提到的那样。 print_r($rsp); 的输出是什么样的?可能 $rsp 是一个包含 1 个元素的数组,在这种情况下,您可以放弃内部循环并将其替换为类似 $pic = $rsp[0]; 的内容。 $desc = $pic['description'];

另外,我会在数据库表中创建一个新的“描述”列(以照片 ID 作为主键),并将描述存储在其上它自己的。像这样解析数据库字段有点像一场噩梦。最后,您可能想强制 htmlspecialchars 在 UTF8 模式下工作,因为我认为默认情况下它不会这样做。根据记忆,第三个参数是内容编码。

编辑: phpFlickr 没有自己的缓存系统吗?为什么不使用它并增大缓存大小?看起来你可能会在这里重新发明轮子......也许你需要做的就是增加缓存大小,并创建一个 getDescription 函数:

function getDescription ($id)
{
    $rsp = $phpFlickr->photos_getInfo ($id);
    $pic = $rsp[0];
    return $pic['description'];
}

I'm not 100% sure as I've just browsed the source for phpFlickr, and looked the the Flickr API for the getInfo() call. But let me have a go anyway :)

First off, it looks like you shouldn't need that loop, like you mention. What does the output of print_r($rsp); look like? It could be that $rsp is an array with 1 element, in which case you could ditch the inner loop and replace it with something like $pic = $rsp[0]; $desc = $pic['description'];

Also, I'd create a new "description" column in your database table (that has the photo id as the primary key), and store the description in their on its own. Parsing db fields like that is a bit of a nightmare. Lastly, you might want to force htmlspecialchars to work in UTF8 mode, cause I don't think it does by default. From memory, the third parameter is the content encoding.

edit: doesn't phpFlickr have its own caching system? Why not use that and make the cache size massive? Seems like you might be re-inventing the wheel here... maybe all you need to do is increase the cache size, and make a getDescription function:

function getDescription ($id)
{
    $rsp = $phpFlickr->photos_getInfo ($id);
    $pic = $rsp[0];
    return $pic['description'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文