我无法从twitter API访问media_url。我知道它在媒体阵列中,我相信媒体阵列是在一个实体数组中。我只是无法正确使用语法。我已经尝试为用户数组创建一个类似的数组,但这不起作用。我也尝试过制作像用户数组一样的实体数组,但是数组变回空了。
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "XXX-XXXXXXXX",
'oauth_access_token_secret' => "XXXXXXXX",
'consumer_key' => "XXXXX",
'consumer_secret' => "XXXX"
);
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = 'https://api.twitter.com/1.1/blocks/create.json';
$requestMethod = 'GET';
/** POST fields required by the URL above. See relevant docs as above **/
$postfields = array(
'screen_name' => 'usernameToBlock',
'skip_status' => '1'
);
/** Perform a POST request and echo the response
e response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23blacklivesmatter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$tweetData = json_decode($twitter->setGetField($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo $tweetData;
foreach($tweetData['statuses'] as $items)
{
$userArray = $items['user'];
$entitiesArray = $items['entities'];
$mediaArray = $entitiesArray['media'];
$date = new DateTime( $items->created_at );
echo "<div class='tweet-div'><div class='float-left twitpic'><a target='_blank' href='http://www.twitter.com/" . $userArray['screen_name'] . "'><img class='twitter-pic' target='_blank' src='" . $userArray['profile_image_url'] . "'></a></div>";
echo "<a target='_blank' href='http://www.twitter.com/" . $userArray['screen_name'] . "'><span class='name bold'>" . $userArray['name'] . "</span> </br><span class='handle'>@" . $userArray['screen_name'] . "</span></a> <span class='font-small'>⋅ ";
echo $date->format( 'M jS' ) . "</span></br>";
echo $items['text'] . "</br></br>";
echo "<img target='_blank' src='http://" . $mediaArray['media_url'] . "'></br></br>";
echo $mediaArray['media_url'] . "";
echo "<span class='border'></span></div>";
}
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>
答案 0 :(得分:0)
使用查询加载推文时,请从此路径获取media_url
$mm1= $tweetData['statuses']["0"]['entities']['media']['0']['media_url'];
echo $mm1;
您将获得附加到推文的(第一)图像的正确数据。如果您有更多图像,可以增加该数字['0']并找到更多。
我必须浪费几个小时才能获得正确的图像以及推文状态(文字) 我还看到一些媒体没有['0'],而是在'媒体'之后指导'media_url'。我的方法可能不是完美的访问所有媒体,但我的目的已经解决。 祝你好运