Twitter REST API:可以热链接Twitter图像吗?为什么在Firefox中没有图像显示? [英] Twitter REST API: Possible to Hotlink Twitter Images? Why No Image Display In Firefox?

查看:55
本文介绍了Twitter REST API:可以热链接Twitter图像吗?为什么在Firefox中没有图像显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Twitter REST API中提取图像并显示这些图像.

困扰我,我的PHP/HTML代码没有在本地PHP内置开发人员服务器的浏览器中显示图像,这导致了逻辑上的僵局.我更喜欢Firefox作为更安全的(私有)浏览器,因此我首先在Firefox中进行开发.

我认为图像未显示的问题也许是Twitter不允许热链接到其图像的问题,但是我只是在Chrome和Internet Explorer浏览器中测试了相同的代码(如下),并且所有图像看起来都很好./p>

所以我的[更新]问题是:为什么这些图像没有出现在Firefox或Firefox Developer Edition浏览器中?Twitter显然不存在与通过其API提取的图像进行热链接的问题,如Chrome和Internet Explorer浏览器中显示的图像都很好地证明了这一点.

谁对这个有趣的问题有答案?

 <!DOCTYPE html>< html lang ="zh-CN">< head><?php//用于调试为什么HTTPS在开始时不起作用//var_dump(stream_get_wrappers());?><?php//需要TWITTEROAUTH库需要"twitteroauth/autoload.php";//我周围有很多东西,但是看不到为什么亚伯拉罕是必需的,因为没有文件夹叫亚伯拉罕,//否则,代码无法正常工作,因此请留在其中!:)使用Abraham \ TwitterOAuth \ TwitterOAuth;//密钥,机密,令牌和&的DECLARE变量TOKEN_SECRET$ CONSUMER_KEY ="12345";$ CONSUMER_SECRET ="12345";$ access_token ="12345";$ access_token_secret ="12345"//定义新的连接变量:即通过TWITTEROAUTH连接到TWITTER$ connection =新的TwitterOAuth($ CONSUMER_KEY,$ CONSUMER_SECRET,$ access_token,$ access_token_secret);//以数组形式创建多参数查询$查询=数组("q" =>#米老鼠","count" =>"1000","include_entities" =>真的");//与推特建立连接,获取搜索/推特"的方法,将查询作为查询通过$ result = $ connection-> get("search/tweets",$ query);//测试的测试输出//var_dump($ result);//创建变量:新的空数组$ ArrayPhotoURLs = array();//对于循环,foreach($ result-> statuss为$ content){//如果每个TWEET/STATUS都有媒体,if(isset($ content-> entities-> media)){//然后获取这些媒体网址foreach($ content-> entities-> media as $ media){$ media_url = $ media-> media_url;//或$ media-> media_url_https作为SSL版本.//并且将每个媒体URL分配/添加到媒体URL的数组中$ ArrayPhotoURLs [] = $ media_url;//测试的测试输出//print(gettype($ media_url));//print_r($ media_url);//var_dump($ media_url);}}}//测试的测试输出//var_dump($ ArrayPhotoURLs);//在twitter图像URL的数组中计数图像-在创建动态列表/锚/IMG项时使用$ imagecount = count($ ArrayPhotoURLs);//测试的测试输出echo $ imagecount;?></head><?php//HTML布局代码从这里开始?>< body style =">< div class ="container">< div><?php//对于循环,将HTML和URL都作为HREF&IMG SRC参数和锚文字foreach($ ArrayPhotoURLs为$ PhotoURL){回声'< a href =',$ PhotoURL,'">','< img src =',$ PhotoURL,'">',$ PhotoURL,'</img></a> br/>';}?></div></div></body></html> 

解决方案

对于具有类似绑定的用户:Firefox显然将域pbs.twimg.com视为跟踪器,并阻止了与该子域的所有连接.考虑在浏览器中禁用跟踪保护,或实施服务器端填充程序以预加载图像.

请参阅: https://bugzilla.mozilla.org/show_bug.cgi?id=1458915

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>

解决方案

For those in a similar bind: Firefox apparently treats the domain pbs.twimg.com as a tracker and blocks all connections made to this subdomain. Consider disabling Tracking Protection in your browser or implement a server-side shim to preload the images.

See: https://bugzilla.mozilla.org/show_bug.cgi?id=1458915

这篇关于Twitter REST API:可以热链接Twitter图像吗?为什么在Firefox中没有图像显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆