如何遍历此Soap响应并回显电话号码和状态,我是JSON对象的Soap响应位很好的新手
<AfricasTalkingResponse>
<SMSMessageData>
<Message>Sent to 2/2 Total Cost: KES 1.6000</Message>
<Recipients>
<Recipient>
<number>+254798834284</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_bbf2f1666edb884ebda0301ba6f3500f</messageId>
<messageParts>1</messageParts>
</Recipient>
<Recipient>
<number>+254719401837</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_e63021c301c9b82aebfe9acc972e49b9</messageId>
<messageParts>1</messageParts>
</Recipient>
</Recipients>
</SMSMessageData>
这是代码
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.africastalking.com/version1/messaging",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"username\"\r\n\r\nFORB\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"to\"\r\n\r\n0719401837,0798834284\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"message\"\r\n\r\nTEst\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"from\"\r\n\r\nFORBES\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Postman-Token: 544432e2-000d-4dc7-af9c-d49c949c6a68",
"apiKey: f2d6ca79d01dbfeee0a1ce42fb82d06d70b8f0ed4ac5f5fb44e11bdb578cd095",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
/* echo $response;
<AfricasTalkingResponse>
<SMSMessageData>
<Message>Sent to 2/2 Total Cost: KES 1.6000</Message>
<Recipients>
<Recipient>
<number>+254798834284</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_bbf2f1666edb884ebda0301ba6f3500f</messageId>
<messageParts>1</messageParts>
</Recipient>
<Recipient>
<number>+254719401837</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_e63021c301c9b82aebfe9acc972e49b9</messageId>
<messageParts>1</messageParts>
</Recipient>
</Recipients>
</SMSMessageData>
</AfricasTalkingResponse>
*/
foreach($response->AfricasTalkingResponse->SMSMessageData->Recipients as $recipient){
echo $recipient->status;
echo $recipient->number;
}
}
现在尝试使用您建议的foreach后的响应为Invalid argument supplied for foreach()
。 IT似乎几乎只需要编辑和查看即可完成。预先感谢
这是答复
<AfricasTalkingResponse>
<SMSMessageData>
<Message>Sent to 2/2 Total Cost: KES 1.6000</Message>
<Recipients>
<Recipient>
<number>+254798834284</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_bbf2f1666edb884ebda0301ba6f3500f</messageId>
<messageParts>1</messageParts>
</Recipient>
<Recipient>
<number>+254719401837</number>
<cost>KES 0.8000</cost>
<status>Success</status>
<statusCode>101</statusCode>
<messageId>ATXid_e63021c301c9b82aebfe9acc972e49b9</messageId>
<messageParts>1</messageParts>
</Recipient>
</Recipients>
</SMSMessageData>
答案 0 :(得分:1)
您必须使用simplexml将其解析为数组:
在SimpleXML中,该对象被视为根元素。因此,您可以像这样遍历收件人项:
$xml = simplexml_load_string($response);
foreach($xml->SMSMessageData->recipients as $recipient)
{
echo (string)$recipient->status;
echo (string)$recipient->number;
}