Skip to content
Advertisement

How to Display plain text of api result ( noob question )

I am completely new to api. I want to display the plain text response of the output of this:

https://vurl.com/api.php?url=https://google.com

to my webpage html but it’s not working.

My code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>

$url = urlencode('https://google.com');
$api_url = 'vurl.com/api.php?url='.$url;
$arr_output = json_decode(file_get_contents($api_url), true);
document.write($arr_output);
</script>

Thanks

Advertisement

Answer

Not really sure why you’re mixing javascript with your php code. Nevertheless, this is the approach you should follow:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>

<?php
$url = urlencode("https://google.com");
$api_url = "vurl.com/api.php?url=".$url;
$arr_output = json_decode(file_get_contents($api_url), true);

echo $arr_output;
?>

</script>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement