Reputation: 23
I have a cURL script that makes connection to a page:
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>
And that page gives an output like:
{
"status" : "fail",
"data" : {
"error_message" : "Destination address 37428963298746238 is invalid for Network=BTC."
}
}
How do I get "error_message" to $error_message
with PHP?
Upvotes: 0
Views: 220
Reputation: 7252
You can get error like that:
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$error = json_decode($output);
dd($error);
?>
Upvotes: 2
Reputation: 3294
This might help you: Pass your output to json_decode
<?php
$json = '{
"status" : "fail",
"data" : {
"error_message" : "Destination address 37428963298746238 is invalid for Network=BTC."
}
}';
$obj = json_decode($json);
foreach($obj as $value){
echo $value->error_message;
}
?>
Upvotes: 0
Reputation: 175
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
// close curl resource to free up system resources
curl_close($ch);
Upvotes: 0