return json data from string

154 Views Asked by At

I hope you can help.

I'm using campaign monitors PHP wrapper.

This is what the call returns:

CS_REST_Wrapper_Result Object ( [response] => HTTP/1.1 202 Accepted Server: csw Cache-Control: private, s-maxage=0 Content-Type: application/json; charset=utf-8 P3P: CP="OTI DSP COR CUR IVD CONi OTPi OUR IND UNI STA PRE" Date: Sun, 14 Feb 2016 13:38:18 GMT X-RateLimit-Limit: 50000 X-RateLimit-Reset: 366 X-RateLimit-Remaining: 49995 Content-Length: 216 [{"MessageID":"2e992358-d320-11e5-b287-c903ee0bee99","Recipient”:”[email protected]”,”Status":"Accepted"},{"MessageID":"2ea9d51f-d320-11e5-9c7a-edc47069cc09","Recipient":"[email protected]","Status":"Accepted"}] [http_status_code] => 202 )  

I want to be able to loop through the "Message ID's" but the [response] is a string containing the header info?

1

There are 1 best solutions below

0
On

A bit weard but thanks to https://stackoverflow.com/a/1445519/1076753 and assuming that you place your request result in a var, this will work

<?php
    $cc='CS_REST_Wrapper_Result Object ( [response] => HTTP/1.1 202 Accepted Server: csw Cache-Control: private, s-maxage=0 Content-Type: application/json; charset=utf-8 P3P: CP="OTI DSP COR CUR IVD CONi OTPi OUR IND UNI STA PRE" Date: Sun, 14 Feb 2016 13:38:18 GMT X-RateLimit-Limit: 50000 X-RateLimit-Reset: 366 X-RateLimit-Remaining: 49995 Content-Length: 216 [{"MessageID":"2e992358-d320-11e5-b287-c903ee0bee99","Recipient”:”[email protected]”,”Status":"Accepted"},{"MessageID":"2ea9d51f-d320-11e5-9c7a-edc47069cc09","Recipient":"[email protected]","Status":"Accepted"}] [http_status_code] => 202 )';

    $startsAt = strpos($cc, '[{"MessageID"');
    $endsAt = strpos($cc, " [http_status_code]", $startsAt);
    $result = substr($cc, $startsAt, $endsAt - $startsAt);
    print_r(json_decode($result));
?>