I know this title is rather confusing, but here it goes.

I have a PHP cURL request and the return is in a string. Normally i can easily echo specific objects from the JSON response, but i have never dealt with string returns.
Here is my php

<?php

$url = "http://profile.combatarms.nexon.net/en/api/ProfilePlayerV2/OG_KneeGro__";
$ch = curl_init($url);

$headr = array();
$headr[] = "Host: profile.combatarms.nexon.net";
$headr[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0";
$headr[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headr[] = "Accept-Language: en-US,en;q=0.5";
$headr[] = "Cookie: i18next=en; _ls_bid=__new__; device_id=a2b3c270-bb08-4afd-8839-4c794e71dded; fingerprintv1=655870065693bdf4ced7825e4fee1f7c4e7f8a37; ccc=US";
$headr[] = "Connection: keep-alive";
$headr[] = "Upgrade-Insecure-Requests: 1";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$res = curl_exec($ch);

echo "<pre>";
echo var_dump($res);
echo "</pre>";

curl_close($ch);

?>

which return this:
string(5283) "executeParce({"d":{"nickname":"xxxxxxx","userstatus":"ACTIVE","createdate":"05/07/2013","nationcode":"US","gradelevel":"55","charactergender":"2","totrank":"5,606",alot more data,}});"

since this is not a JSON return, how do i get the nickname?

Here is the request header and response header from firefox.

Response:

Cache-Control   "private"
Content-Type    "application/x-javascript; charset=utf-8"
Server  "Microsoft-IIS/7.5"
X-AspNetMvc-Version "5.2"
X-AspNet-Version    "4.0.30319"
Set-Cookie  "pageLan=1nDyHvJz5W8=; domain=…n; domain=.nexon.net; path=/"
x-powered-by    "ASP.NET"
Date    "Sun, 06 Aug 2017 05:50:51 GMT"
Content-Length  "5283"

Request:

Host    "profile.combatarms.nexon.net"
User-Agent  "Mozilla/5.0 (Windows NT 10.0;… Gecko/20100101 Firefox/54.0"
Accept  "text/html,application/xhtml+x…lication/xml;q=0.9,*/*;q=0.8"
Accept-Language "en-US,en;q=0.5"
Accept-Encoding "gzip, deflate"
Cookie  "i18next=en; _ls_bid=__new__; …825e4fee1f7c4e7f8a37; ccc=US"
DNT "1"
Connection  "keep-alive"
Upgrade-Insecure-Requests   "1"
Cache-Control   "max-age=0"

Hi,

that's a JSONP response, so in order to process this through PHP you need to remove the callback function that wraps the JSON data, for example:

callback({JSON DATA});

At this point you can remove it from the string:

<?php

$jsonp = 'callback({"name": "micheal"});';
$callb = 'callback'; // to remove

$s = substr($jsonp, mb_strlen($callb) + 1); // +1 to include the opening `(`
$s = substr($s, 0, -2); // -2 to remove `);`

print_r(json_decode($s, true));

Now, most JSON servers allows the client to define a callback in the requesting link:

http://url/page.php?callback=foo

So you receive:

foo({JSON DATA});

This allows you to write a more robust solution, as it's should not affect your script if they change their default callback function. See also if the server allows to get other formats, like simple JSON or XML.

Thanks cereal, once you said that it was jsonp, i was able to refine my search. In the end, i came across this function that seem to work really well:

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}

print_r(jsonp_decode($res, true));

This was a featured post when i donated, but i dont know why its not showing as one. Thanks again for all your help and pointing me in the right direction.

commented: You're welcome! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.