How to use php curl to obtain a json object and convert it into an array

不言
Release: 2023-03-28 17:24:01
Original
2836 people have browsed it

This article mainly introduces the method of php curl to obtain json objects and convert them into arrays. It has certain reference value. Now I share it with you. Friends in need can refer to it

Example:

function objtoarr($obj){
$ret = array();
foreach($obj as $key =>$value){
if(gettype($value) == 'array' || gettype($value) == 'object'){
$ret[$key] = objtoarr($value);
}else{
$ret[$key] = $value;
}
}
return $ret;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://www.tudou.com/albumcover/albumdata/getAlbumItems.html?acode=pEFBZGfERLo&charset=utf-8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
$output = curl_exec($ch);
curl_close($ch);
$content = json_decode($output);
$content_arr = objtoarr($content);
var_dump($content_arr);
Copy after login

The above is the entire content of this article, thank you for reading. Please pay attention to the PHP Chinese website for more information!

Related recommendations:

php curl simulates login and obtains data instance

php curl batch processing to achieve controllable concurrent asynchronous operation case details

The above is the detailed content of How to use php curl to obtain a json object and convert it into an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template