0

I doing a post curl request

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
        curl_setopt($ch, CURLOPT_POST, 1);
        $headers = array();
        $headers = ["Content-Type:application/json","Accept:application/json"];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close ($ch);
        return $result;

The above curl request should return a json formated string but getting a string in javascript object form.

string(68) "{data:{errorCode:AC01,errorMessage:SansID 53563857 is exist.}}"

Where as when i try to do the same thing from post man api is returning perfect json.

{"data":{"errorCode":"AC01","errorMessage":"SansID 53563857 is exist."}}

Please let me know where i'm doing wrong.

user1837779
  • 575
  • 1
  • 5
  • 17

1 Answers1

0

Use json_decode to convert String into Object (stdClass) or array: I was having the same issue working with WordPress(cURL) and Laravel(passport) powered API that was returning JSON but in string format. I saved the response

//save json string into variable json object and return result
$response = curl_exec($curl);
//convert t
return (json_decode($response));

refer to this link here for a similar solution

Check out this screenshot console logs for before and after the fix