-2

这是我创建的函数,用于通过 cURL auth 获取 Delicious 最近的书签,然后进行 XML->JSON 转换:

<?php
// JSON URL which should be requested
$json_url = 'https://api.del.icio.us/v1/posts/recent';

$username = 'myusername';  // authentication
$password = 'mypassword';  // authentication

// Initializing curl
$ch = curl_init( $json_url );

// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $username . ":" . $password   // authentication
);

// Setting curl options
curl_setopt_array( $ch, $options );



$cache_delicious = '/BLAHBLAH/'.sha1($json_url).'.json';

    if(file_exists($cache_delicious) && filemtime($cache_delicious) > time() - 1000){
        // if a cache file newer than 1000 seconds exist, use it
        $data_delicious = file_get_contents($cache_delicious);
    } else {

        $delicious_result = simplexml_load_string(curl_exec($ch));
        $data_delicious = json_encode($delicious_result);
        file_put_contents($cache_delicious, $data_delicious);
    }

$obj = $data_delicious['post']['@attributes'];

foreach (array_slice(json_decode($data_delicious, true), 0, 5) as $obj) {
    $delicious_title = str_replace('"', '\'', $obj['description']);
    $delicious_url = htmlentities($obj['href'], ENT_QUOTES, "UTF-8");
    $output = "<li><a rel=\"external nofollow\" title=\"$delicious_title\" href=\"$delicious_url\">$delicious_title</a></li>";
    echo $output;
}
?>

这是 JSON,如果我执行 print_r($data_delicious);,为了便于阅读,只减少到一个条目:

{
   "@attributes":{
      "tag":"",
      "user":"myusername"
   },
   "post":[
      {
         "@attributes":{
            "description":"Fastweb: fibra o VDSL? Disinformazione alla porta",
            "extended":"",
            "hash":"d00d03acd6e01e9c2e899184eab35273",
            "href":"http:\/\/storify.com\/giovannibajo\/fastweb-fibra-o-vdsl",
            "private":"no",
            "shared":"yes",
            "tag":"",
            "time":"2013-06-14T10:30:08Z"
         }
      }
   ]
}

不幸的是,变量($delicious_title$delicious_url)有问题foreach,因为我得到未定义的索引:描述和href。

4

3 回答 3

0

尝试使用json-last-error来捕获错误

于 2013-06-14T11:07:54.013 回答
0

如果您阅读了json_decode 的手册,您可以看到第二个参数。如果将其设置为 true,则输出将是一个数组。所以只需使用array_slice(json_decode($data_delicious, true), 0, 5).

最好事先进行一些错误检查。

<?php
$result = json_decode($data_delicious, true);
if (is_array($result)) {
  foreach (array_slice($result, 0, 5) as $obj) {
    $delicious_title = str_replace('"', '\'', $obj->description);
    $delicious_url = htmlentities($obj->href, ENT_QUOTES, "UTF-8");
    $output = "<li><a rel=\"external nofollow\" title=\"$delicious_title\" href=\"$delicious_url\">$delicious_title</a></li>";
    echo $output;
  }
}

?>
于 2013-06-14T11:11:03.497 回答
0

错误清楚地说

your variable obj is undefined

尝试这个

$obj_delicious = json_decode($data_delicious, true); 

foreach (array_slice($obj_delicious, 0, 5) as $obj)
    {        $delicious_title = str_replace('"', '\'', $obj['post']['@attributes']['description']);
            $delicious_url = htmlentities($obj['post']['@attributes']['href'], ENT_QUOTES, "UTF-8");
            $output = "<li><a rel=\"external nofollow\" title=\"$delicious_title\" href=\"$delicious_url\">$delicious_title</a></li>";
            echo $output;
    }
        }
于 2013-06-14T11:18:36.613 回答