【问题标题】:unable to access data from soap api response json无法从soap api响应json访问数据
【发布时间】:2018-07-19 12:43:14
【问题描述】:

我正在使用soap api请求并返回给我this response.

这是我的 laravel 控制器代码。

    public function testResponseSRI($phone, $code)
{
    $url = 'https://tamimahsms.com/user/bulkpush.asmx?wsdl';
    $body = '
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <SendSMS xmlns="https://www.tamimahsms.com/">
    <UserName>username</UserName>
    <Password>password</Password>
    <Message>test message with code '.$code.'</Message>
    <Priority>1</Priority>
    <Schdate>07/18/2018</Schdate>
    <Sender>AsifSource</Sender>
    <AppID>123456</AppID>
    <SourceRef>7654321</SourceRef>
    <MSISDNs>'.$phone.'</MSISDNs>
    </SendSMS>
    </soap:Body>
    </soap:Envelope>';

    $headers = ['Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($body)];
    $result = SoapClientRequest::send($url, $headers, $body);
    return response()->json($result);
}

我想从body 对象中获取&lt;StatusCode&gt;00&lt;/StatusCode&gt;。 我怎样才能得到这个?

谢谢

【问题讨论】:

    标签: api laravel-5 soap soap-client


    【解决方案1】:

    终于,我想通了。响应不是普通的 JSON,先转换一下:

                $data = $jsonResponse->getData();
    

    然后将其视为常规 json。现在我可以通过使用 php preg_match 轻松获得&lt;StatusCode&gt;00&lt;/StatusCode&gt;

    这样的方法:

    if(preg_match("!<StatusCode>(.+)</StatusCode>!i",$data->body,$matches)) 
    {
        return $matches[1]      // return 00
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多