Jump to content

PHP get cURL response value as variable


jarvis

Recommended Posts

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

Link to comment
Share on other sites

You can get the value. You're already doing it.

$json->success
The 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.