请帮我解决代码
{“success”:“true”,“type”:“API”,“totalprice”:“1”,“totalgsm”:“1”,“remainingcredit”:“491短信”,“结果”:[ {“status”:“OK”,“messageid”:“263db4bc-321f-5d2e-8d3a-454bf0d2bda2”,“gsm”:“923100000003”}]}
在我的PHP代码中,我想检查一下,如果值返回为 true ,它应该运行第一个if语句,如果值返回为 false < / strong>它应该运行else语句。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cpurl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec ($ch);
if (preg_match("/successfully/", $result ))
{
$alert = '<div class="alert alert-success">
<button type="button" class="close"
data-dismiss="alert" aria-hidden="true">×</button>
<strong>Messages successfuly sent to
'.$cnt.' Students.</strong>
</div>';
$status = 'Success';
}else{
$alert = '<div class="alert alert-danger">
<button type="button" class="close"
data-dismiss="alert" aria-hidden="true">×</button>
<strong>Message Sending Failed.</strong>
</div>';
$status = 'Failed';
}
答案 0 :(得分:0)
您可以使用JsonMapper来解决任务。只需定义一个对象(视图模型),它将是你json的php表示。
答案 1 :(得分:0)
为什么不使用json_decode
?
....
$result = curl_exec ($ch);
$response = json_decode($result);
现在您可以访问$response->success
:
if ($response->success === "true") {
$alert = ' × Messages successfuly sent to '.$cnt.' Students. ';
$status = 'Success';
} else {
$alert = ' × Message Sending Failed. '; $status = 'Failed';
}