0

So what I'm doing is using the Twitter RestAPI to get the picture of a user and just be able to display the image on a local page. I've figured out the way to grab the URL of the image and this is what I have so far:

   $url = $result->profile_image_url;

This holds the url of the profile picture of the user. Now how do I use this url to display it on the page? I know HTML allows us to do something like :

   <img src = "some url">

And it will display the picture on the web page. I've looked around the internet for a while now, and still haven't figured it out. I've run into the same suggestion of the following :

   <img src = "<?php echo ($url); ?>"/>

But I keep getting a parse error when I attempt loading the page. I would really appreciate if someone could guide me/help me figure out what's going on. Clearly, I'm doing something wrong which is why the PHP interpreter is not able to parse the code. Thanks!

Here is my whole code (if that helps):

<html>
 <body>

   <?php
     require_once('TwitterAPIExchange.php');
     $settings = array(
         'oauth_access_token' => "N/A",
         'oauth_access_token_secret' => "N/A",
         'consumer_key' => "N/A",
         'consumer_secret' => "N/A"
     );

     $url = 'https://api.twitter.com/1.1/users/show.json';
     $getfield = '?screen_name=kobebryant';
     $requestMethod = 'GET';

     $twitter = new TwitterAPIExchange($settings);
     $json = $twitter->setGetfield($getfield)
            ->buildOauth($url, $requestMethod)
            ->performRequest();

    $result = json_decode($json);

    $url = $result->profile_image_url;
    <img src = "<?php echo ($url);?>"/> //This is the line giving me an error
     ?>
 </body>
 </html>
8
  • 3
    nothing wrong with that php snippet. you'd have to show what other code comes before/after. Commented Jul 22, 2015 at 18:18
  • Just wrote the code up there. Let me know what you think man. Thanks a bunch! Commented Jul 22, 2015 at 18:24
  • you have no ?>, so </body> is going to be seen as less than divide by undefined-constant-body Commented Jul 22, 2015 at 18:25
  • Sorry, I must have forgotten to write that in the code above. I have the proper close php brackets on my text editor. I attempted doing the following as well (just to test it out) and it still hasn't worked. $temp = "img01.deviantart.net/69c9/i/2013/071/f/e/…"; <img src = "<?php echo ($temp); ?>"/> And I get a parse error Commented Jul 22, 2015 at 18:28
  • @halapgos1 What exactly is the error message you get? Commented Jul 22, 2015 at 18:34

1 Answer 1

2

You closed the php tag too late. That is why it is recommended to split php from html. Try this code below:

<?php
    require_once('TwitterAPIExchange.php');
    $settings = array(
        'oauth_access_token' => 'N/A',
        'oauth_access_token_secret' => 'N/A',
        'consumer_key' => 'N/A',
        'consumer_secret' => 'N/A'
    );

    $url = 'https://api.twitter.com/1.1/users/show.json';
    $getfield = '?screen_name=kobebryant';
    $requestMethod = 'GET';

    $twitter = new TwitterAPIExchange($settings);
    $json = $twitter->setGetfield($getfield)
        ->buildOauth($url, $requestMethod)
        ->performRequest();

    $result = json_decode($json);

    $url = $result->profile_image_url;
?>
<html>
    <head></head>
    <body>
        <img src="<?php echo ($url);?>"/> //This is the line giving me an error       
    </body>
</html>

Hint: if you've just a string without variables use ' instead of " because php tries to parse content between " and this is waste of CPU power.

Sign up to request clarification or add additional context in comments.

5 Comments

Okay it works! Thanks! I'm fairly new to PHP so is this the way to go about it in terms of the PHP coding convention? To write the PHP on top and then then html? Or does that not matter? I definitely want to be using correct PHP coding convention.
Yes, I think that is a better way. When you continue the way of PHP and come in contact with MVCs you will work with html templates and there you have to split php and html anyway.
Okay, cool thanks man! Another question, why can we use the $url variable inside the html tags? Isn't it out of scope? Because the php tags ended before that already?
When you close this php part with the closing tag it does not mean that you lose the whole scope. You could even include another php file in the top line of this file and write there all the php stuff and it would still work. It is more like open php tag = enter php realm, close php tag = leave php realm ... but if you open it again, you are still back in the old php realm.
Oh, I see. That analogy makes a lot more sense. Thank you so much for your help! Have an amazing day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.