i need to display/show photo with twitter api, but i have only get 1 photo
$results = $toa->get('https://api.twitter.com/1.1/statuses/show.json?id=xxxxxxxxxxxx' );
if (isset($results->entities->media)) {
foreach ($results->entities->media as $media) {
$media_url = $media->media_url; // Or $media->media_url_https for the SSL version.
}
}
echo $media_url;
this code get only 1 photo ,how to get pulling multiple image (max 4) on 1 twitter status ?
thank
It looks like you're overwriting the $media_url variable on each loop. You would want to loop through the $results->entities->media variable when displaying the images like this:
foreach ($results->entities->media as $media) {
echo '<img src="'.$media->media_url.'"/>';
}
I don't know too much about the Twitter API however you are only getting one photo at the end because you are doing this.
Inside the loop you keep updating $media_url to $media->media_url. Then outside the loop you echo $media_url only once.
All you are doing is changing that one variable over and over, you need to have your echo statement inside the loop.
if (isset($results->entities->media)) {
foreach ($results->entities->media as $media) {
$media_url = $media->media_url;
echo $media_url;
}
}
Like I said I don't have too much knowledge of the Twitter API but providing your loop is working to get one of the images then moving the echo statement inside the loop should help.
Related
I've been recently building a plugin for wordpress which basically uses the instagram API to get an image URL and then place it in a short code.
And I've come to a problem.
I get this error:
E_WARNING : type 2 -- Invalid argument supplied for foreach() -- at
line 22
and I have no idea what am I doing wrong.
My code for the foreach:
//define Access token
$accesst= "ACCESS_TOKEN_GOES_HERE";
//userid
$userid=USERID_GOES_HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'][0]['images']['standard_resolution']['url'] as $photo)
{
print $photo['url'][0];
echo "<br>";
}
My JSON var_dump got me this:
https://pastebin.com/3RaL6EUA
The access codes, were of course deleted before posting this.
Does anyone have a clue what I'm doing wrong?
EDIT:
Thanks, everyone, got it figured out in the comments.
Your $standardres['data'] have items which have images, so you must use $standardres['data'] in the foreach loop and then parse the image url from the item data.
foreach($standardres['data'] as $item) {
print $item['images']['standard_resolution']['url'];
echo "<br>";
}
I don't know exactly the hierarchy of the instagram API, but I suggest you to try :
foreach($standardres['data']['images'] as $photo) {
print_r($photo); // to see what the array contains!
echo $photo['standard_resolution']['url'];
echo "<br>";
}
I have a small function that grabs the avatars of comment authors on a specified video. It simply loops through the JSON data returned by YouTube API v3 commentThreads method.
The only issue is that sometimes an author has commented more than once, so my function is displaying the authors avatar more than once. I'd like to only display it one time, and on to the next avatar.
Here's a picture of what I mean:
Currently my function looks like this:
function videoCommentAvatars($video) {
// Parse YouTube video ID from the url
if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $video, $match)) {
$video_id = $match[1];
}
// Gather Video stats with YouTube API v3
$api_key = "API_KEY_HERE";
$JSON = file_get_contents('https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId='.$video_id.'&key='.$api_key);
$json_data = json_decode($JSON, true);
if (!empty($json_data)) {
foreach ($json_data['items'] as $data) {
// Create variables that hold info
$author_name = $data['snippet']['topLevelComment']['snippet']['authorDisplayName']; // Author Name
$author_avatar = $data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; // Author Avatar
$author_channel = $data['snippet']['topLevelComment']['snippet']['authorChannelUrl']; // Author Channel URL
echo '<span class="comment-author-avatar">';
echo '<a target="_blank" href="'.$author_channel.'" title="'.$author_name.'"><img width="50" alt="'.$author_name.'" class="comment-author-thumb-single" src="'.$author_avatar.'"></a>';
echo '</span>';
}
}
}
Everything works fine, but there's no way to check if an avatar has been displayed yet. I thought about using an array maybe? Adding each avatar URL to the array, and checking the array to see if the key exists. But that seems like overkill for something that's seemingly more simple. Does anyone have a clever way of checking the foreach loop for duplicates?
to check for duplicates in an array, you have a few options. Firstly, to get rid of any before looping, you can use array_unqiue($array) which will return an array that does not repeat it's values.
Or if you do need initial access to all values in the loop, and to do something if a repeat is present, you can have another array that acts as a record to see if they appear more than once.
$record = array();
foreach ($json_data['items'] as $data) {
// Create variables that hold info
$author_name = $data['snippet']['topLevelComment']['snippet']['authorDisplayName']; // Author Name
if(!in_array($author_name, $record)){
$author_avatar = $data['snippet']['topLevelComment']['snippet']['authorProfileImageUrl']; // Author Avatar
$author_channel = $data['snippet']['topLevelComment']['snippet']['authorChannelUrl']; // Author Channel URL
echo '<span class="comment-author-avatar">';
echo '<a target="_blank" href="'.$author_channel.'" title="'.$author_name.'"><img width="50" alt="'.$author_name.'" class="comment-author-thumb-single" src="'.$author_avatar.'"></a>';
echo '</span>';
$record[] = $author_name;
}
}
You can try a multi array with a unique index in your loop.
$author[$data['snippet'][...]['authorDisplayName']['avatar'] = $data['snippet'][...]['authorProfileImageUrl'];
So only unique results in the array.
I am using Simple_html_dom trying to grab Reputation points and make a check on those values and then extract only those user profiles whose Repu is greater that 200. Below is the code, It works fine when doing this
`
$html2=str_get_html($html1);
foreach ($html2->find('.user-details') as $value) {
foreach($html2->find('.reputation-score') as $repu){
echo (int)$repu->plaintext.$value.'<br>';
}
}
When doing the above i am getting all thereputation and the user detailswell each line, looks fine till now. But now i wanna perform aconditional checkand then echo only those profiles whose repo is higher than100`. So when i am trying the below code
`
$html2=str_get_html($html1);
foreach ($html2->find('.user-details') as $value) {
foreach($html2->find('.reputation-score') as $repu){
if($repu>100)
{
echo ($repu->plaintext.$value.'<br>';
}
}
}
I am getting an error saying Object of class simple_html_dom_node could not be converted to int seeing that i've tried doing (int) to convert and then echo still couldnt solve
Any help here ?
I want to display the last 100 followers on Twitch using their API on my website. However, I know very little about JSON.
Currently I have this:
$string = file_get_contents("https://api.twitch.tv/kraken/channels/pewdiepie/follows.json?limit=100");
$json=json_decode($string,true);
But I can't get it in a while loop.
I tried this:
foreach ($json as $key => $value){
echo $value['name'];
}
Can someone help me? I just want the last 100 follower names displayed on the page.
Assuming that getting the data works, this should do it:
foreach ($json['follows'] as $follow){
echo $follow['user']['name'];
}
I'm using CMS ProscessWire.
I've a working foreach loop that shows child's title and 5 links. The links are shown as images. This works flawless. But then I want for an empty link a black and white image to show there's no link. So i made a if statement. But when i put the if statement inside the loop the only child that is shown is the one with an empty link.
Does anyone know how tot resolve this? I think it's a simple thing, but can't figure it out.
PHP-Code:
$rdio='rdio.png';
$itunes='itunes.png';
$xbox='xbox.png';
$googleplay='googleplay.png';
$spotify='spotify.png';
$deezer='deezer.png';
foreach($page->children as $child)
if(!$child->Rdio) {
$rdio='rdiog.png';
}
else
{
$rdio='rdio.png';
}
echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";
You are missing some brackets around foreach which only executes the following IF statement and continues to print out a single element on your page. To correct this, just add brackets around the foreach loop like this:
$rdio='rdio.png';
$itunes='itunes.png';
$xbox='xbox.png';
$googleplay='googleplay.png';
$spotify='spotify.png';
$deezer='deezer.png';
foreach($page->children as $child) {
if(!$child->Rdio) {
$rdio='rdiog.png';
}
else
{
$rdio='rdio.png';
}
echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";
Try using empty method:
$rdio='rdio.png';
$itunes='itunes.png';
$xbox='xbox.png';
$googleplay='googleplay.png';
$spotify='spotify.png';
$deezer='deezer.png';
foreach($page->children as $child) {
if(empty($child->Rdio)) {
$rdio='rdiog.png';
}
else
{
$rdio='rdio.png';
}
echo "<ul style='background-color:#CCC;'>"."<li><h1>{$child->title}</h1></li>".<li><a class='rdio' href='{$child->Rdio}'><img src='../site/templates/img/$rdio' alt='rdio'/></a></li>"."<li><a class='itunes' href='{$child->iTunes}'><img src='../site/templates/img/$itunes' alt='itunes'/></a></li>"."<li><a class='xbox' href='{$child->Xbox}'><img src='../site/templates/img/$xbox' alt='xbox'/></a></li>"."<li><a class='googleplay' href='{$child->GooglePlay}'><img src='../site/templates/img/$googleplay' alt='googleplay'/></li>"."<li><a class='spotify' href='{$child->Spotify}'><img src='../site/templates/img/$spotify' alt='spotify'/></a></li>"."<li><a class='deezer' href='{$child->Deezer}'><img src='../site/templates/img/$deezer' alt='deezer'/></a></li>"."</ul><br/>";