Mandeep Gill
Mandeep Gill

Reputation: 4907

GuzzleHttp\Client JSON Return as String

I have very simple question, how to convert this strings to JSON ?

This string i am getting from Guzzle POST Request and here is code :

return $body->getBody()->getContents();

Result:

""k\n\n{\"success\":true,\"payload\":{\"id\":\"txn_ngS2aS9FY7raxy8JTUivAZCtWJy7EeznwPE8\"}}""

with var_dump result and what is that k before ?

string(79) "k {"success":true,"payload":{"id":"txn_eeM6T6Fvkq3Pr4AWtK2TKYmNwKmodNwVqJod"}}"

Upvotes: 3

Views: 265

Answers (2)

Mandeep Gill
Mandeep Gill

Reputation: 4907

I found problem isn't in my code but it was in external api payload, payload sending json data with "k" string.

Thanks @Ozan Kurt to figure out external link.

So right now only picking curly brackets with regex :

$result = preg_match("/{.+}", $body->getBody()->getContents(), $matched);

if ($matched) {
  return json_decode($matched[0]);
}

Upvotes: 0

Ozan Kurt
Ozan Kurt

Reputation: 3861

The string is already json_encoded, you should simply return the $body->getBody()->getContents().

Upvotes: 1

Related Questions