Get JSON object from URL in PHP
This article explains how to get a JSON object from a URL in PHP.
file_get_contents()
Get JSON object from URL in PHP
using function
We can use the README.mdl file_get_contents()
and README.mdl json_decode()
to get a JSON object from a URL. file_get_contents()
The README.mdl function reads a file in string format. We should specify the path of the file in the function or we can even pass the URL in the function as the first argument. We should enable README.mdl allow_url_fopen
to use file_get_contents()
the README.mdl function. We can enable it by php.ini
setting README.mdl in our README.mdl file . The README.mdl function converts a JSON object into a PHP object. So, we can access the object in the JSON URL as a PHP object.phpini_set("allow_url_fopen", 1)
json_decode()
For demonstration, we will use a dummy JSON URL from jsonplaceholder. Create a variable $url
and store the URL in it. Use the URL https://jsonplaceholder.typicode.com/posts/1
. The JSON object for the URL is shown below. Next, create a $json
variable and use $url
as file_get_contents()
a parameter of the function. Now, json_decode()
decode the JSON string into a PHP object using the function. Store the object in the variable. Finally, access the object $jo
using and print it out.$jo
title
So, we accessed a URL from the Web that contained a JSON object and converted it to PHP. This way, we can get the JSON object from the URL in PHP.
Sample code:
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
<?php
$url = 'https://jsonplaceholder.typicode.com/posts/1';
$json = file_get_contents($url);
$jo = json_decode($json);
echo $jo->title;
?>
Output:
sunt aut facere repellat provident occaecati excepturi optio reprehenderit
curl
Get JSON object from URL in PHP
using
curl
is a command line tool used to send and receive data and files. It uses supported protocols like HTTP, HTTPS, FTP, etc. and sends data from or to a server. In PHP, there is a curl
library that allows us to make HTTP requests. We can use to curl
read file contents from the network. There are various curl
functions in PHP that facilitate us to send and receive data. We can use them to get a JSON object from a URL. curl_init()
The curl function starts curl. We can use curl_setopt()
the function to set several options like returning the transport and setting the URL. curl_exec()
The curl function performs the action and curl_close()
closes curl.
We can demonstrate the use of using the same URL as in the first method curl
. Create a variable $curl
and curl_init()
start it with the function curl
. Use curl_setopt()
the function to CURLOPT_RETURNTRANSFER
set the option to true
. Next, CURLOPT_URL
set the URL with the option. Execute curl with curl_exec()
the function and the arguments $curl
and store them in $res
the variable. Use curl_close()
the function to close $curl
the variable. Next, use json_decode()
the function to change the JSON object to a PHP object and display title
the object.
Therefore, we can use curl
to get the JSON object from the URL.
Sample code:
<?php
$curl= curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://jsonplaceholder.typicode.com/posts/1';
$res = curl_exec($curl);
curl_close($curl);
$jo = json_decode($res);
echo $jo->title; ?>
Output:
sunt aut facere repellat provident occaecati excepturi optio reprehenderit
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Check if a Post exists in PHP
Publish Date:2025/04/13 Views:170 Category:PHP
-
PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss
PHP with Ajax
Publish Date:2025/04/13 Views:139 Category:PHP
-
We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Returns the article tag with ID from the action page
Publish Date:2025/04/13 Views:80 Category:PHP
-
Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at
Switching PHP versions on Ubuntu
Publish Date:2025/04/13 Views:78 Category:PHP
-
Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t
Resizing images in PHP
Publish Date:2025/04/13 Views:155 Category:PHP
-
In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using
PHP upload image
Publish Date:2025/04/13 Views:61 Category:PHP
-
We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image
Creating a signature from Hash_hmac() and Sha256 in PHP
Publish Date:2025/04/13 Views:107 Category:PHP
-
PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i
Updating PHP 7.x to 7.4 on CentOS
Publish Date:2025/04/13 Views:131 Category:PHP
-
This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of