K
K
Konstantin2016-11-13 14:28:01
PHP
Konstantin, 2016-11-13 14:28:01

How to get json data from string?

Good afternoon!

It is required to receive json data sent by the server. The server returns a response in the form of a string:
"HTTP 200 OK.... a lot of any text ... [{data1, data2},{data3, data4}]"

I need to isolate {data1, data2}, {data3, data4} and write through the loop to the $result array, like:
$result[] = array(
'parametr1' => data1,
'parametr2' => data2,
);
I repeat, the answer comes in the form of a single line, not an object

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
frees2, 2016-11-13
@frees2

$fff2 ='https://www.googleapis.com/youtube/v3/search'.$region.'&'.$vpoisk.'&key=///////////////'.$rating.'&type=video&'.$time.'&part=snippet&safeSearch=none'. $kolisestvo.'
';

        

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $fff2);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP Bot');
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_exec($ch); 
  $json = curl_exec($ch);
  curl_close($ch);
  if ($json !== false) { 
$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
// setlocale(LC_ALL, 'ru_RU.utf8');
 $json = json_decode($json, true) ; 
 // print_r($json);
 

 
 if (isset($json['items']))
 {

 foreach($json['items'] as $items) {
$channelId =$items['snippet']['channelId'];
  
  
$publishedAt =$items['snippet']['publishedAt'];
$title =$items['snippet']['title'];
if (isset($items['snippet']['thumbnails']['default']['url']))
 { $thumbnails =$items['snippet']['thumbnails']['default']['url'];

 if (isset($items['id']['videoId']))  { 


 
 $videoId =$items['id']['videoId'];

A
Anton Bobylev, 2016-11-13
@dpigo

You need to parse the string. Either regular expressions or string methods, depending on the structure of the string, and then convert the desired section to JSON or map.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question