php - json_decode is returning a null value instead of an array -
i trying search keyword email in decoded data. not able array type json_decode.
here code
$curl = curl_init(); curl_setopt_array($curl, array( curlopt_returntransfer => true, curlopt_url => $url, curlopt_useragent => 'mozilla/5.0 (macintosh; intel mac os x 10_9_2) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36' )); $resp = json_decode(curl_exec($curl), true); if(is_array($resp) && array_key_exists("email", $resp)) { echo $data_arr[0] . "email: "; $content = $resp["email"]; fwrite($fp,$content); }
the exact error is:
array_key_exists() expects parameter 2 array, null given in index.php on line 34
edit: figured out error somewhat. curl execution fails due error ( url malformed). still not able figure out how url malformed in case. url extracted such.
$data = fgets($fp); $data_arr = split(",", $data); $token_arr = isset($data_arr[1]) ? split('"', $data_arr[1]) : null; $url = isset($token_arr[1]) ? "https://graph.facebook.com/v2.3/me?access_token=" . $token_arr[1] : null;
try use utf8_encode
$data = curl_exec($curl); $data = utf8_encode($data); $resp = json_decode($data, true);
note:
utf8_decode
work utf8this function works utf-8 encoded strings.
php implements superset of json specified in original » rfc 4627 - encode , decode scalar types , null. rfc 4627 supports these values when nested inside array or object. although superset consistent expanded definition of "json text" in newer » rfc 7159 (which aims supersede rfc 4627) , » ecma-404, may cause interoperability issues older json parsers adhere strictly rfc 4627 when encoding single scalar value.
encode arguments in url needed, try urlencode
or rawurlecnode
, example:
note, added
()
in? :
$data = fgets($fp); $data_arr = split(",", $data); $token_arr = isset($data_arr[1]) ? split('"', $data_arr[1]) : null; $url = isset($token_arr[1]) ? ("https://graph.facebook.com/v2.3/me?access_token=" . urlencode($token_arr[1])) : null;
check if send null
if curl_error
return malformed
meaning $url = null
, problem in code generates token, try this:
$data = fgets($fp); $data_arr = split(",", $data); $token_arr = isset($data_arr[1]) ? split('"', $data_arr[1]) : null; $url = isset($token_arr[1]) ? ("https://graph.facebook.com/v2.3/me?access_token=" . $token_arr[1]) : null; if (empty($url)) { echo 'url null or empty'; exit; } $curl = curl_init(); curl_setopt_array($curl, array( curlopt_returntransfer => true, curlopt_url => $url, curlopt_useragent => 'mozilla/5.0 (macintosh; intel mac os x 10_9_2) applewebkit/537.36 (khtml, gecko) chrome/43.0.2357.81 safari/537.36' )); $data = curl_exec($curl); $data = utf8_encode($data); $resp = json_decode($data, true); if(is_array($resp) && array_key_exists("email", $resp)) { echo $data_arr[0] . "email: "; $content = $resp["email"]; fwrite($fp,$content); }
Comments
Post a Comment