问题
I am trying to extract images from Twitter REST API and display those images.
Insodoing I have come to a logical impasse with my PHP/HTML code not displaying the images in the browser of my localhost PHP built-in developer server. I prefer Firefox as more secure (private) browser, so I first develop in Firefox.
I thought that the issue of images not displaying was perhaps issue of Twitter not allowing hotlinking to their images, however I just tested this same code (below) in both Chrome and Internet Explorer browsers, and all the images appear fine.
So my [updated] question is: why are these images not appearing in either Firefox or Firefox Developer Edition browsers? Twitter clearly doesn't have an issue with hotlinking to their images that have been extracted via their API as demonstrated by images showing up fine in both Chrome and Internet Explorer browsers.
Who has the answer to this interesting issue?
<!DOCTYPE html>
<html lang="en">
<head>
<?php
// USED TO DEBUG WHY HTTPS WAS NOT WORKING IN THE BEGINNING
// var_dump(stream_get_wrappers());
?>
<?php
// REQUIRE TWITTEROAUTH LIBRARY
require "twitteroauth/autoload.php";
// I HAVE HACKED AROUND WITH THIS, BUT DON'T SEE WHY ABRAHAM IS NECESSARY SINCE THERE IS NO FOLDER NAMED ABRAHAM,
// BUT CODE DOES NOT WORK WITHOUT THIS, SO LEAVE IT IN! :)
use Abraham\TwitterOAuth\TwitterOAuth;
// DECLARE VARIABLES OF KEYS, SECRET, TOKEN, & TOKEN_SECRET
$CONSUMER_KEY = "12345";
$CONSUMER_SECRET = "12345";
$access_token = "12345";
$access_token_secret = "12345"
// DEFINE NEW CONNECTION VARIABLE: I.E. CONNECTION TO TWITTER VIA TWITTEROAUTH
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $access_token, $access_token_secret);
// CREATE MULTIPLE-PARAMETER QUERY AS ARRAY
$query = array(
"q" => "#MickeyMouse",
"count" => "1000",
"include_entities" => "true"
);
// MAKE CONNECTION TO TWITTER, GET METHOD FOR "SEARCH/TWEETS", PASS ARRAY AS QUERY
$result = $connection->get("search/tweets", $query);
// TEST OUTPUT FOR DEBUGGING
//var_dump($result);
// CREATE VARIABLES: NEW EMPTY ARRAYS
$ArrayPhotoURLs = array();
// FOR LOOP,
foreach ($result->statuses as $content) {
// IF EACH TWEET/STATUS HAS MEDIA,
if (isset($content->entities->media)) {
// THEN GET THOSE MEDIA URLS
foreach ($content->entities->media as $media) {
$media_url = $media->media_url; // Or $media->media_url_https for the SSL version.
// AND ASSIGN/APPEND EACH MEDIA URL TO THE ARRAY OF MEDIA URLs
$ArrayPhotoURLs[] = $media_url;
// TEST OUTPUT FOR DEBUGGING
//print(gettype($media_url));
//print_r($media_url);
//var_dump($media_url);
}
}
}
// TEST OUTPUT FOR DEBUGGING
//var_dump($ArrayPhotoURLs);
// COUNT IMAGES IN ARRAY OF TWITTER IMAGE URLS - TO BE USED BELOW TO CREATE DYNAMIC LIST / ANCHOR / IMG ITEMS
$imagecount = count($ArrayPhotoURLs);
// TEST OUTPUT FOR DEBUGGING
echo $imagecount;
?>
</head>
<?php // HTML LAYOUT CODE BEGINS HERE ?>
<body style="">
<div class="container">
<div>
<?php
// FOR LOOP, PRINT HTML WITH URL AS BOTH A HREF & IMG SRC PARAMETERS & ANCHOR TEXT
foreach ($ArrayPhotoURLs as $PhotoURL) {
echo '<a href="', $PhotoURL ,'">',
'<img src="',$PhotoURL, '">', $PhotoURL,
'</img></a><br/>';
}
?>
</div>
</div>
</body>
</html>
来源:https://stackoverflow.com/questions/41500995/twitter-rest-api-possible-to-hotlink-twitter-images-why-no-image-display-in-fi