jarvis Posted August 29, 2017 Share Posted August 29, 2017 Hi All, I'm hoping some kind soul can assist. I have the following php script: #initialize cURL $ch = curl_init(); #connect to the remote URL curl_setopt($ch, CURLOPT_URL, $url); #tell curl_exec to return the response output as a string curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); #instruct cURL to do a regular HTTP POST curl_setopt($ch, CURLOPT_POST, 1); #specify the data which is to be posted curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); #execute the cURL session if($result = curl_exec($ch) === false) { die('Curl error: ' . curl_error($ch)); } $ch_result = curl_exec($ch); The result returned is as follows: { "message":"lorem ipsum dolor", "success":false, "errors": } What I'm trying to do (with no luck) is to get the value of success. I thought I could do something like: $json = json_decode($ch); echo $json->status; But that doesn't work What am I doing wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/ Share on other sites More sharing options...
requinix Posted August 29, 2017 Share Posted August 29, 2017 Actually it does work. false as a string is empty so you don't see anything. Try using print_r or var_dump instead. Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550313 Share on other sites More sharing options...
jarvis Posted August 29, 2017 Author Share Posted August 29, 2017 Hi Requinix Many thanks for the reply. I've tried: $json = json_decode($ch); echo '<hr>'.$json->success; print_r($json); However, that returns nothing Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550315 Share on other sites More sharing options...
requinix Posted August 29, 2017 Share Posted August 29, 2017 It won't "return" anything. What is the output you see? Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550318 Share on other sites More sharing options...
jarvis Posted August 29, 2017 Author Share Posted August 29, 2017 Thanks I guess what I'm trying to do is get the value (true/false) so I can action something depending on the result. Perhaps that helps shed light on what I'm trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550320 Share on other sites More sharing options...
requinix Posted August 29, 2017 Share Posted August 29, 2017 You can get the value. You're already doing it. $json->successThe problem is that your attempts to view that value are not working: you cannot simply echo it because the value is false and, like I said, false->string (which happens for everything being echo-ed) is an invisible empty string; if it were true you would have seen a 1. Meanwhile print_r... oh dammit, I forgot. You can't use print_r() either. It doesn't do the same false->empty string that echo does, but it does happen to also print out nothing for a false value. For ~reasons~. Try var_dump() instead. That always prints out something. Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550324 Share on other sites More sharing options...
jarvis Posted August 29, 2017 Author Share Posted August 29, 2017 Oh I see - my apologies. Ok, I now see NULL, so guess I can test either NULL or 1 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550325 Share on other sites More sharing options...
Solution requinix Posted August 29, 2017 Solution Share Posted August 29, 2017 No, that's not the problem. I skipped the first half of your post and went straight to the second part, where you said you had some JSON and couldn't get the success value. I didn't notice that you don't actually have the JSON. cURL will output stuff by default. That is what your code is doing now: outputting the JSON to the browser. That's how you're able to see what it is. Instead, you need to get that output into a variable. You do it with the CURLOPT_RETURNTRANSFER option. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);When that is set, the return value from curl_exec (which you have as $ch_result) will be the response as a string. The JSON. You then need to decode that variable. Not $ch, which is just a cURL identifier for doing cURL operations, and useless once you've curl_close()d it. Which, by the way, you need to do if you aren't doing it already. $json = json_decode($ch_result);And now you can properly use var_dump to see the value you want: var_dump($json->success);It will say either bool(true) or bool(false) - anything else probably means there's still an error in your code. If you think something should be true/false and you get null then don't just roll with it. Find out why it's doing that and not doing what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550327 Share on other sites More sharing options...
jarvis Posted August 29, 2017 Author Share Posted August 29, 2017 Hi, Perhaps I wasn't clear - so I do apologies! I have one final question though. If the output is: { "message":"Lorem ipsum.", "success":false, "errors":[{ "error_code":"VAL_00149", "error_value":"lorem", "error_description":"lorem" I now know I can use: $status = $json->success; This returns 1 or NULL (great - thank you!) If I want to get the error description, is that possible? I assume something like: $errors = $json->errors; echo 'Errors: ' . $errors[0]['errors'] . '<br />'; Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550329 Share on other sites More sharing options...
jarvis Posted August 29, 2017 Author Share Posted August 29, 2017 Apologies, I realised I needed: echo $json->errors['errors']['error_description']; Many thanks for all your help! Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550330 Share on other sites More sharing options...
requinix Posted August 29, 2017 Share Posted August 29, 2017 Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550331 Share on other sites More sharing options...
jarvis Posted September 1, 2017 Author Share Posted September 1, 2017 Hi Just following up on this as I've had to amend the code, to: $json = json_decode($ch_result, true); As such, if I now dump out the info I get something like: Array { [warnings] => [time] => [errors] => Array { [message] => Lorem ipsum } } How can I access the message? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550569 Share on other sites More sharing options...
jarvis Posted September 1, 2017 Author Share Posted September 1, 2017 Please ignore, I sorted it $message = $json['errors']['message']; Quote Link to comment https://forums.phpfreaks.com/topic/304758-php-get-curl-response-value-as-variable/#findComment-1550572 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.