Answer the question
In order to leave comments, you need to log in
How to get data from JSON in PHP if the object name starts with a number?
Hello colleagues. I apologize right away if I don’t express myself correctly regarding json, because little known to him.
The point is this. There is a Wordpress API that returns data in json.
There is php that accesses the server via curl and receives data in json.
I am trying to output this data, however the json object name starts with a number. How to be?
This is what I ask
$googleApiUrl = "https://clivir.ru/wp-json/wp/v2/posts";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $googleApiUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$currentTime = time();
$title = ucwords($data->0[0](название объекта)->title->rendered) . "\n";
Answer the question
In order to leave comments, you need to log in
$endpoint = 'https://clivir.ru/wp-json/wp/v2/posts';
$data = json_decode(file_get_contents($endpoint), true);
foreach ($data as $datum) {
$title = $datum['title']['rendered'];
$content = $datum['content']['rendered'];
// ...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question