Jorge Anzola
Jorge Anzola

Reputation: 1235

Set current url + string in img src

I'm trying to create a QR code based on current URL using goqr.me API: https://api.qrserver.com/v1/create-qr-code/?size=150x150&data= so I need generate this code <img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=**currentURL**">

Things I've tried:

1)

<?php 
    function getUrl() { 
    $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"]; 
    $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; 
    echo '<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data='.$url;
    } ?>

2)

<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php function getUrl() { 
     $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"]; 
     $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; echo $url;} ?>">

As you can see, I'm not very good at PHP programming. I really hope you can help me.

Upvotes: 1

Views: 315

Answers (1)

Varis
Varis

Reputation: 254

In you second example you declare the function and don't call it, so nothing gets written.

The second example can be changed to this:

<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?php function getUrl() { 
 $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"]; 
 $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; echo $url;} getUrl(); ?>">

or it would look better like this:

<?php 
function getUrl() { 
    $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"]; 
    $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : ""; $url .= $_SERVER["REQUEST_URI"]; 
    return $url;
} 
?>

<img src="https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=<?= getUrl(); ?> ">

Upvotes: 1

Related Questions