PHP exec响应作为字符串? [英] PHP exec response as string?
问题描述
我目前通过exec()使用已安装的curl版本,而不是使用curl_exec()查询API(linkedin)。
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.
这是我的代码:
exec('curl "'.$url.'/v1/people/~?format=json" -H "Authorization: Bearer '.$token.'"', $data);
我得到的响应是(我用xxxx替换了敏感数据):
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 = api xxxx xxxx *}
[6] => string(1)}} / p>
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) "}" }
我尝试过 json_encode(json_decode($ data))
不改变任何东西...如何将这个输出转换成JSON对象?
I've tried json_encode(json_decode($data))
but it doesn't change anything... How can I transform this output into a JSON object?
推荐答案
我发布它lol。无论如何,如果有人面临同样的问题,我解决它:
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);
这篇关于PHP exec响应作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!