为什么不从flickr显示中获取图像? [英] Why won't the images that are grabbed from flickr display?
问题描述
我正在学习在项目中使用flickr api的过程中,并且遇到了问题...在flickr检索照片数据的地方遇到了问题(如下所示)
I'm in the proccess of learning the flickr api, while using it in a project, and have got a problem... I have it where flickr retrieves the photo data (shown below)
但是它本身不会显示图像...我认为这是由于它没有使用下面的src=" "
信息构建<img />
标签的事实,这是我尝试使用的代码. ..
but it doesn't display the images themselves... I think this is due to the fact that it's not building the <img />
tags with the src=" "
info below is the code that I'm attempting to use...
(我的class.flickr.php代码文件)
(my class.flickr.php code file)
class Flickr{
private $flickr_key;
private $flickr_secret;
private $format = 'json';
// Setting up flickr_key and flickr_secret
public function __construct( $flickr_key ) {
$this->flickr_key = $flickr_key;
}
public function searchPhotos( $query = '', $tags = '' ){ // Begin searchPhotos
$urlencoded_tags = array( 'animals', 'design', 'phones');
if ( !empty( $args )) {
$tags_r = explode( ',', $tags );
foreach ( $tags_r as $tag ) {
$urlencoded_tags[] = urlencode( $tag );
}
}
// Construct the url
$url = 'http://api.flickr.com/services/rest/?';
$url .= 'method=flickr.photos.search';
$url .= '&text=' . urlencode( $query );
$url .= '&tags=' . implode( ',', $urlencoded_tags );
$url .= '&sort=relevance';
$url .= '&safe_search=1';
$url .= '&content_type=4';
$url .= '&api_key=' . $this->flickr_key;
$url .= '&format' . $this->format;
$url .= '&per_page=10';
// Calling url using curl
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_TIMEOUT => 120,
CURLOPT_URL => $url,
));
if ( !curl_exec( $curl )) {
die ( 'Error: "' . curl_error( $curl ) . '" - Code: ' . curl_errno( $curl ));
}
// Get search results
$result = file_get_contents( $url );
// Remove the unneccessary strings that wraps the result returned from the API
$json = substr( $result, strlen( "jsonFlickrApi("), strlen( $result ) - strlen( "jsonFlickrApi(") - 1 );
$photos = array();
$data = json_decode( $json, true );
// Check if the status didn't fail
if ( $data['stat'] != 'fail' ) {
/** Return only the data for the photos
as that's the only thing that we need
*/
$photos = $data['photos']['photo'];
return $photos;
} else {
return false;
}
} // end searchPhotos
}
(我在页面模板文件中使用的方法调用)
(my method call which I use in my page template file)
<?php // Flickr search photos test
require_once( 'class.flickr.php' );
global $flickr_key;
$flickr = new Flickr( 'this_is_my_api-key_placeholder...' );
$query = "Event Photo Uploadr";
$results = $flickr->searchPhotos( $query, $tag );
if ( !empty( $results )) {
foreach( $results as $photo ) {
$src = "http://farm" . $photo['farm'] . ".static.flickr.com/" . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '.jpg';
?>
<img src="<?php echo $src; ?>"/>
<?php }
}
正如我所说的,我是flickr api的新手...到目前为止,我很幸运能做到这一点(感谢stackoverflow,Wordpress Answers和开发者朋友),因此,非常感谢任何有用的输入! ;)
As I said I'm new to flickr api... I've been blessed to make it thus far (thanks to stackoverflow, Wordpress Answers and developer friends), so any useful input is GREATLY appreciated! ;)
推荐答案
好的,这听起来很愚蠢,但是,真正的问题是我错过了=
.我不是在开玩笑,我有这个$url .= '&format' . $this->format;
(使api,默认情况下返回xml)应该是这个$url .= '&format=' . $this->format;
(这是获取我的$format
var的正确方法).我还整理了一下代码...
Okay, this is going to sound silly, but, the real problem was I was missing a =
. I'm not joking, I had this $url .= '&format' . $this->format;
(which made the api, by default return xml) should be this $url .= '&format=' . $this->format;
(this is the proper way to grab my $format
var). I also cleaned up my code a bit...
(主要的flickr功能文件)
(main flickr functionality file)
<?php
class Flickr{
private $flickr_key;
private $flickr_secret;
private $format = 'json';
// Setting up flickr_key and flickr_secret
public function __construct( $flickr_key ) {
$this->flickr_key = $flickr_key;
}
public function searchPhotos( $query = '', $tags = '' ){ // Begin searchPhotos
$urlencoded_tags = array( 'animals', 'design', 'phones');
if ( !empty( $args )) {
$tags_r = explode( ',', $tags );
foreach ( $tags_r as $tag ) {
$urlencoded_tags[] = urlencode( $tag );
}
}
// Construct the url
$url = 'http://api.flickr.com/services/rest/?';
$url .= 'method=flickr.photos.search';
$url .= '&text=' . urlencode( $query );
$url .= '&tags=' . implode( ',', $urlencoded_tags );
$url .= '&sort=relevance';
$url .= '&safe_search=1';
$url .= '&content_type=4';
$url .= '&api_key=' . $this->flickr_key;
$url .= '&format=' . $this->format;
$url .= '&per_page=10';
// Get search results
$result = file_get_contents( $url );
// Remove the unnecessary strings that wraps the result returned from the API
$json = substr( $result, strlen( "jsonFlickrApi(" ), strlen( $result ) - strlen( "jsonFlickrApi(" ) - 1 );
$photos = array();
$data = json_decode( $json, true );
// Check if the status didn't fail
if ( $data['stat'] != 'fail' ) {
// Return only the data for the photos as that's the only thing that we need
$photos = $data['photos']['photo'];
return $photos;
} else {
return false;
}
} // end searchPhotos
}
我在方法调用中所做的唯一更改是添加了一个alt标记(如alt="<?php echo $photo['title']?>"
这样),并向else语句echo "Failed to construct url successfully";
添加了一个错误信息.
The only things that I changed in my method call was adding an alt tag, like so alt="<?php echo $photo['title']?>"
, and adding a error messege to the else statement, echo "Failed to construct url successfully";
.
这篇关于为什么不从flickr显示中获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!