Parse JSON Freebase results in PHP

为君一笑 提交于 2020-01-01 19:27:16

问题


I'm really sorry if this is too basic, but I really don't know how to do this.

I'm using this jquery Autocomplete plugin: http://devthought.com/wp-content/projects/jquery/textboxlist/Demo/

EDIT: This is the jquery code i use for the autocomplete:

$(function() {
        var t = new $.TextboxList('#form_topick_tags', {unique: true, plugins: {autocomplete: {
                minLength: 2,
                queryRemote: true,
                remote: {url: 'autocomplete2.php'}
            }}});

The plugin uses a PHP for autocomplete, this is an example, it returns this output: "id, text, null (html I don't need), some html"

$response = array();
            $names = array('Abraham Lincoln', 'Adolf Hitler', 'Agent Smith', 'Agnus', 'Etc');

            // make sure they're sorted alphabetically, for binary search tests
            sort($names);

            $search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

            foreach ($names as $i => $name)
            {
                if (!preg_match("/^$search/i", $name)) continue;
                $filename = str_replace(' ', '', strtolower($name));
                $response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
            }

            header('Content-type: application/json');
            echo json_encode($response);

I need a similar PHP to process this results: http://www.freebase.com/private/suggest?prefix=beatles&type_strict=any&category=object&all_types=false&start=0&limit=10&callback=

...being "beatles" the $search value, and getting this output:

guid,"name",null,"name<span>n:type name</span>"

So, the first result would be:

0,"The Beatles",null,"The Beatles<span>Band</span>"

Of course I would need to query freebase.com from that PHP. I mean:

        +---------------+         +-----------+        +------------+
        |               |         |           |        |            |
        |  TextboxList  +-------->|   PHP     +------->|  Freebase  |
        |               |         |           |        |            |
        +---------------+         +-----------+        +------+-----+
                                                              |
             JSON                     JSON                    |
          TextboxList   <--------+  freebase       <----------+

Is this possible? Thanks!


回答1:


Try this:

$response = array();

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : '';

$myJSON = file_get_contents('http://www.freebase.com/private/suggest?prefix=' . urlencode($search));

$musicObj = json_decode($myJSON); // Need to get $myJSON from somewhere like file_get_contents()

foreach ($musicObj->result as $item)
{
    $response[] = array($item->guid, $item->name, null, $item->name . '<span>'.$item->{'n:type'}->name.'</span>');
}

header('Content-type: application/json');
echo json_encode($response);

The first JSON-escaped result then gives:

["#9202a8c04000641f800000000003ac10","The Beatles",null,"The Beatles<span>Band<\/span>"]

But despite all this, you really don't need to use PHP at all to do this. You can do this all from JavaScript and avoid an extra trip to your server. If you supply the callback argument to freebase, it can create JSONP (which is JSON wrapped in a call to a function, using a function name of your choice) which you can obtain in jQuery and then manipulate further in JavaScript to your liking. But the above is per your original approach in using PHP.



来源:https://stackoverflow.com/questions/6352942/parse-json-freebase-results-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!