- This topic is empty.
-
AuthorPosts
-
January 16, 2014 at 1:30 pm #160551chrisburtonParticipant
foreach ($response as $result) { if($result['provider'] == 'Facebook') { $user = array( 'provider' => $result['provider'], 'id' => $result['uid'], 'name' => $result['info']['name'], 'image' => $result['info']['image'], 'link' => $result['info']['urls']['facebook'], ); echo "<h1>".$user['provider']."</h1>"; echo "<p>".$user['id']."</p>"; echo '<p><img src="'.$user['image'].'" /></p>'; echo "<p>".$user['name']."</p>"; echo "<p>".$user['link']."</p>"; exit; }elseif($result['provider'] == 'Twitter') { $user = array( 'provider' => $result['provider'], 'id' => $result['uid'], 'name' => $result['info']['name'], 'image' => $result['info']['image'], 'link' => $result['info']['urls']['twitter'], ); echo "<h1>".$user['provider']."</h1>"; echo "<p>".$user['id']."</p>"; echo '<p><img src="'.$user['image'].'" /></p>'; echo "<p>".$user['name']."</p>"; echo "<p>".$user['link']."</p>"; exit; }else{ header('Location: http://chrisburton.me/'); exit; } }
The only difference in both if/elseif conditions are these two
'link' => $result['info']['urls']['facebook'] 'link' => $result['info']['urls']['twitter']
I tried combining them and creating an if like this but it didn’t work (also tried if/elseif).
$user = array( 'provider' => $result['provider'], 'id' => $result['uid'], 'name' => $result['info']['name'], 'image' => $result['info']['image'], if($result['provider'] == 'Facebook') { 'link' => $result['info']['urls']['facebook'], }if($result['provider'] == 'Twitter') { 'link' => $result['info']['urls']['twitter'], } );
January 16, 2014 at 2:13 pm #160557AlenParticipantI’m not able to test this at the moment but mabe someting like this, where you determine what provider is responding then just assign a variable to that field.
foreach ($response as $result) { if ( $result['provider'] == 'Facebook' ) { $provider = 'facebook'; } else { $provider = 'twitter'; } $user = array( 'provider' => $result['provider'], 'id' => $result['uid'], 'name' => $result['info']['name'], 'image' => $result['info']['image'], 'link' => $result['info']['urls'][$provider], ); echo "<h1>".$user['provider']."</h1>"; echo "<p>".$user['id']."</p>"; echo '<p><img src="'.$user['image'].'" /></p>'; echo "<p>".$user['name']."</p>"; echo "<p>".$user['link']."</p>"; exit; }
NOTE: I haven’t tested this code at all. Just quickly rewrote it.
Hope it helps.
January 16, 2014 at 2:40 pm #160562chrisburtonParticipantUnfortunately that doesn’t work. And of course, my VPS isn’t spitting out errors like it should. Just a blank page. Thanks for trying, Alen. Much appreciated.
January 16, 2014 at 3:05 pm #160570AlenParticipantTry it with using quotes around $provider in
$result['info']['urls'][$provider]
January 16, 2014 at 3:07 pm #160571chrisburtonParticipantI don’t think that’s allowed in PHP, no? I’ll try it, though.
Edit: Didn’t work.
January 16, 2014 at 3:19 pm #160572AlenParticipantUsing double quotes?
January 16, 2014 at 3:20 pm #160573chrisburtonParticipantNope that wouldn’t work either. But you were right the first time. It was another piece of code below that which was causing the error. Thanks for the solution, Alen.
January 17, 2014 at 12:42 am #160607stevetParticipantJust my thoughts…
foreach ($response as $result) { echo "<h1>".($result['provider']=='Facebook'?'Facebook':'Twitter')."</h1>". "<p>".$result['uid']."</p>". '<p><img src="'.$result['info']['image'].'" /></p>'. "<p>".$result['info']['name']."</p>". "<p>".($result['provider']=='Facebook'?$result['info']['urls']['facebook']:$result['info']['urls']['twitter'])."</p>"; exit; // not sure what the exit is for in a loop. }
January 17, 2014 at 12:46 am #160608chrisburtonParticipantThanks @stevet. Although your solution may work, it is beyond my understanding.
// not sure what the exit is for in a loop.
The reason I have exit is because my code continues to loop through, spitting out the same values multiple times.
January 17, 2014 at 4:40 am #160625chrisburtonParticipantFigured out along with another person from SO that the foreach loop was throwing a fatal error if I didn’t use
break;
after the last echo.Because I am obtaining one user at a time, a foreach loop is probably not what I should be using. Instead I can just do:
$result = $response['auth']; if ( $result['provider'] == 'Facebook' ) { $provider = 'facebook'; } else { $provider = 'twitter'; } $user = array( 'provider' => $result['provider'], 'id' => $result['uid'], 'name' => $result['info']['name'], 'image' => $result['info']['image'], 'link' => $result['info']['urls'][$provider], ); echo "<h1>".$user['provider']."</h1>"; echo "<p>".$user['id']."</p>"; echo '<p><img src="'.$user['image'].'" /></p>'; echo "<p>".$user['name']."</p>"; echo "<p>".$user['link']."</p>";
January 17, 2014 at 1:15 pm #160662TheDocMemberI like hearing progress!
January 17, 2014 at 4:35 pm #160680chrisburtonParticipantThanks for the support, Gray!
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.