0

As I've been taught here, don't ever cut it off or mess with strings. decode the JSON and extract the relevant property

If you response is like that:

\"request_time":"2021-07-11 04:52:07\",\"elapsed_return_time_milliseconds\":26.65,\"access_ip\":null,\"error\":null,\"amount_sent\":1}

Means that you need decode with

$json = json_decode($string, true);

This is my updated code to get values, where $inicio contains the JSON response:

    $inicio = curl_exec($ch);
$json = json_decode($inicio, true);
$gettime = $json['header']['request_time'];

                echo json_encode(array("status" => 1, "msg" => "Error $number - Empty Response"));
            }else{
               echo json_encode(array("status" => 0, "msg" => "Ok $Number - $reqtime Response"));
            }

The output now is:

Ok 123456 - $2021-07-11 04:52:07 Response

So if I need get request_time value from this response:

 "header": {
    "api_version": "3.0.0",
    "type_connection": 1,
    "user": "",
    "request_time": "2021-07-11 05:46:24",
    "elapsed_return_time_milliseconds": 28.34,
    "access_ip": null,
    "error": null,
    "amount_sent": 1
  },

I can use this:

$gettime = $json['header']['request_time'];

Thank you very much! Please formulate the answer so I can mark as correctly

Marcio
  • 1
  • 3
  • Don't cut it off or mess with strings. It looks like JSON so decode the JSON and extract the relevant property – ADyson Jul 11 '21 at 08:10
  • Could you show me more with an short example? Please formulate an answer – Marcio Jul 11 '21 at 08:13
  • Hang on, perhaps I misunderstood a little bit, are you saying it's a raw string before you json_encode it? It wasn't very clear what value you are starting off with? What is the original value of `$inicio`? – ADyson Jul 11 '21 at 08:19
  • The value of `$inicio` is this json response `\"request_time":"2021-07-11 04:52:07\",\"elapsed_return_time_milliseconds\":26.65,\"access_ip\":null,\"error\":null,\"amount_sent\":1}` I need print only request_time and if `$reqtime` is empty send as json_encode with status 1, else if it's empty send as json_encode with status 0 – Marcio Jul 11 '21 at 08:30
  • Inside `echo json_encode(array(` I need print the response previously stored in string `$reqtime` (it already occours but it print entire response and I need cutoff unecessary part, As you said I should decode JSON and extract but don't figure out how to do – Marcio Jul 11 '21 at 08:39
  • Please add the full json object that you get to your question. The one you posted (My json response) seems to be truncated. – digijay Jul 11 '21 at 08:42
  • `this json response`... But what you've shown us isn't valid JSON. Did you omit part of it? – ADyson Jul 11 '21 at 08:47
  • 1
    Updated the question with full response, I shorten it because it was too big. About not being a JSON valid response maybe it was because this bars? \ \ If was it that you mean, I don't know why but the response appears like this with a lot \ \ \ when I print using the variable `$reqtime` Accessing the response by URL, I get the regular response (the same fully response that I've updated on question) Thank you all guys in advance It's really cool receive multiple supports like that – Marcio Jul 11 '21 at 08:58
  • then you need to decode the JSON, extract the date value to a variable and then re-use it in your later output. Does this answer your question? [How do I extract data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – ADyson Jul 11 '21 at 08:59
  • P.s. The backslashes ( \ ) were added by you when you tried to encode something to json which was contained double quotes to begin with. – ADyson Jul 11 '21 at 09:01
  • @ADyson already tried a similar solution but it not worked when I try to print the response of $reqtime variable inside echo json_encode(array( Do you know if is possible decode the JSON and print the variable value inside an encode? – Marcio Jul 11 '21 at 09:04
  • I will read this topic and try it again, probably I've missed something Really appreciate your help @ADyson Thank you! – Marcio Jul 11 '21 at 09:06
  • Yes it's definitely possible to do what you described. Once you've decoded it it's just a PHP variable like any other. Maybe show us exactly what you tried already – ADyson Jul 11 '21 at 09:09
  • I tried to get value of request_time from response like that: `$json = json_decode($inicio, true); $gettime = $json[0]['header']['request_time'];` the request_time value is inside header array, is this code correct ? If yes, I will update my question and you can formulate a answer since my problem must be solved decoding json but when I try print $gettime I get an empty value – Marcio Jul 11 '21 at 09:14
  • `is this code correct `... No I don't think so because the original JSON is an object not an array so there won't be a [0] index. You can check the structure with var_dump – ADyson Jul 11 '21 at 09:21
  • Just try `$json['header']['request_time'];` directly – ADyson Jul 11 '21 at 09:24
  • Oh my god, it worked!! I was trying with [0] that was the problem... I've updated my question, can you formalize the answer? so we done here after that, i believe i can do it alone i will try to learn more and more really thank you for your patience – Marcio Jul 11 '21 at 09:48
  • Hello again @ADyson, I already finish all variables, is just pretty print and follow copying variables. Thanks to you. Just formulate a answer with `$json['header']['request_time'];` – Marcio Jul 11 '21 at 12:22

0 Answers0