Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec response as string?

Tags:

json

php

curl

I currently use the installed version of curl via exec() instead of using curl_exec() to query an API (linkedin). My call returns the good result but is weirdly encoded and I cannot transform it back to json.

Here is my code:

exec('curl "'.$url.'/v1/people/~?format=json" -H "Authorization: Bearer '.$token.'"', $data);

The response I get is (i replaced sensitive data with xxxx):

array(7) { [0]=> string(1) "{" [1]=> string(22) " "firstName": "xxxx"," [2]=> string(67) " "headline": "xxxx;"," [3]=> string(21) " "id": "xxxx"," [4]=> string(24) " "lastName": "xxxx"," [5]=> string(148) " "siteStandardProfileRequest": {"url": "https://www.linkedin.com/profile/view?id=xxxx&authType=name&authToken=xxxx&trk=apixxxxxxxx*"}" [6]=> string(1) "}" }

I've tried json_encode(json_decode($data)) but it doesn't change anything... How can I transform this output into a JSON object?

like image 958
Eric Avatar asked Nov 16 '25 08:11

Eric


1 Answers

Why do I find the answer once I post it lol. Anyway just in case someone faces the same issue, I solved it with:

$data = implode('', $data);
$data = json_decode($data);
like image 73
Eric Avatar answered Nov 18 '25 23:11

Eric