N
N
noname_20192019-04-04 09:39:45
PHP
noname_2019, 2019-04-04 09:39:45

Interpreting CURL results, how?

Good day to all. Faced with an interesting situation, I would like your help.
I'm just starting to delve into Web programming (experience no more than 4 months), doing everything I have to do (layout, js, php, etc.).
Essence of the question.
I have the following php code:

<?php
  $ch = curl_init();
  $url="url скрыт";
  $param="{\r\n\"Request_id\": \"необходимый id\",\r\n\"Method\": \"getCities\",\r\n\"Parameters\": {\r\n}\r\n}\r\n";


  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
//	curl_setopt($ch, CURLOPT_CERTINFO,true);
  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);


  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch, CURLOPT_USERPWD,"login:password");
  curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");


  $result = curl_exec($ch); 
  
  var_dump($result);

  
  curl_close($ch);
  
  
?>

This request is sent to the server, from which we receive a list of cities available on the basis. The response in $result looks like this:
string(267) "HTTP/1.1 200 OK Date: Thu, 04 Apr 2019 06:31:15 GMT Server: Apache Content-Length: 142 Content-Type: application/json { "Request_id": "id", "Parameters": [ "City1", "City2" ], "Error": 0 }"
Actually, the whole problem is to extract everything that is in the Parameters array from this response.
The only way I've come up with so far is to manually parse and split using strpos, substr, and str_replace. But there is a problem with other requests, for example, instead of the getCities method there will be a getCars method, which will obviously have more characteristics than one (and, it turns out, the parameters array will contain more arrays inside itself, etc.). Are there other ways that are better and easier?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanKud, 2019-04-04
@noname_2019

Why do you need headers in the output? Remove the headers and just parse the JSON:

.....
curl_setopt($ch, CURLOPT_HEADER, false);
.....

$arr = json_decode($result, true);
print_r($arr);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question