我正在整合Facebook API以获取用户个人资料详细信息...在使用try catch获取Facebook响应时。
try {
$request = new FacebookRequest($this->session, 'GET', '/' . $user);
$response = $request->execute();
$results = $response->getGraphObject()->asArray();
} catch(FacebookRequestException $fx) {
//echo "requested " . $user . " - " . $fx->getCode();
//echo $fx->getMessage();
unset($results);
$results = null;
}
print_r($results);
return $results;
问题是,当FACEBOOK上不存在$ user时,来自graphobject的FB响应返回错误代码803。 在这种情况下,我试图将其作为异常捕获,并将$ results设置为null。
但是,在这种情况下,$ result永远不会为null或永远不会设置为null。
预期的行为是:
调用FB API,如果结果良好,则返回数组。如果结果不好, 处理错误并返回null。
[编辑] 控制器:
$ cover = $ this-> facebook-> get_profile($ brand_facebook); $ picture = $ this-> facebook-> get_picture($ brand_facebook);
print_r($cover);
if(!is_null($cover)) {
$brand->cover = $cover['cover']->source;
}
if(!is_null($picture))
$brand->picture = $picture['url'];
}
get_profile($ brand_facebook);应该有错误并返回null,但它会返回以下内容:
Array
(
[<!DOCTYPE_html>
<html_lang] => "en" id="facebook">
<head>
<title>Facebook | Error</title>
<meta charset="utf-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="cache-control" content="no-store">
<meta http-equiv="cache-control" content="max-age=0">
<meta http-equiv="expires" content="-1">
<meta http-equiv="pragma" content="no-cache">
<meta name="robots" content="noindex,nofollow">
<style>
...
我做错了什么?
答案 0 :(得分:0)
unset($results); //<-- Not necesary
$results = null;
你不需要这两行...使用$ result = null就足够了。但是我会这样做以实现预期的行为。
try {
$request = new FacebookRequest($this->session, 'GET', '/' . $user);
$response = $request->execute();
$results = $response->getGraphObject()->asArray();
//Since you are not using a special handle for this type of error
//let's use just a generic exception and see if you get the expected
//behavior
} catch(Exception $fx) {
//echo "requested " . $user . " - " . $fx->getCode();
//echo $fx->getMessage();
return null; //Returns null on error
}
return $results;