Jump to content

PHP: GET JSON from URL and structure it in HTML


Zorglub

Recommended Posts

I have this code:

<?php
//The URL of the resource that is protected by Basic HTTP Authentication.
$url = 'https://gw.bilinfo.net/listingapi/api/export';

//Your username.
$username = 'demo';
//Your password.
$password = 'ocfB6XzF73';

//Initiate cURL.
$ch = curl_init($url);
 
//Specify the username and password using the CURLOPT_USERPWD option.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

//Tell cURL to return the output as a string instead
//of dumping it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Execute the cURL request.
$response = curl_exec($ch);
 
//Check for errors.
if(curl_errno($ch)){
    //If an error occured, throw an Exception.
    throw new Exception(curl_error($ch));
}

//Print out the response.
echo $response;
?>

But how do I put it into HTML though?

Link to comment
Share on other sites

Example...

<?php
//The URL of the resource that is protected by Basic HTTP Authentication.
$url = 'https://gw.bilinfo.net/listingapi/api/export';

//Your username.
$username = 'demo';
//Your password.
$password = 'ocfB6XzF73';

//Initiate cURL.
$ch = curl_init($url);
 
//Specify the username and password using the CURLOPT_USERPWD option.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

//Tell cURL to return the output as a string instead
//of dumping it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Execute the cURL request.
$response = curl_exec($ch);
 
//Check for errors.
if(curl_errno($ch)){
    //If an error occured, throw an Exception.
    throw new Exception(curl_error($ch));
}

?>
<html>
<head>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
</head>
<body>

<?= $response; ?>

</body>
</html>

 

Link to comment
Share on other sites

This a example without authentication:

<?php
$jsonurl = "https://reqres.in/api/users/2";
$json = file_get_contents($jsonurl);
$jsonDecode = json_decode($json, true);
echo "<b>E-mail:</b> " . $jsonDecode['data']['email'];
echo $jsonDecode['data']['first_name'];
?>

 

Link to comment
Share on other sites

51 minutes ago, Barand said:

This example just takes the $response, and put it all in there. I need to control the individual cars so I can style them...

51 minutes ago, Barand said:
<?php
//The URL of the resource that is protected by Basic HTTP Authentication.
$url = 'https://gw.bilinfo.net/listingapi/api/export';

//Your username.
$username = 'demo';
//Your password.
$password = 'ocfB6XzF73';

//Initiate cURL.
$ch = curl_init($url);
 
//Specify the username and password using the CURLOPT_USERPWD option.
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);  

//Tell cURL to return the output as a string instead
//of dumping it to the browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Execute the cURL request.
$response = curl_exec($ch);
 
//Check for errors.
if(curl_errno($ch)){
    //If an error occured, throw an Exception.
    throw new Exception(curl_error($ch));
}

?>
<html>
<head>
<meta http-equiv="content-language" content="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
</head>
<body>

<?= $response; ?>

</body>
</html>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.