我有一个连接到页面的cURL脚本:
<?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);
?>
该页面的输出如下:
{
"status" : "fail",
"data" : {
"error_message" : "Destination address 37428963298746238 is invalid for Network=BTC."
}
}
如何使用PHP将“error_message”传递给$error_message
?
答案 0 :(得分:1)
你可能会收到这样的错误:
$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);
?>
答案 1 :(得分:0)
// 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);
答案 2 :(得分:0)
这可能会对您有所帮助:将您的输出传递给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;
}
?>