Home  >  Article  >  Backend Development  >  How to use php curl to obtain a json object and convert it into an array

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

不言
不言Original
2018-05-31 14:41:492778browse

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);

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!

Statement:
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