无法使用PHP在JSON中获取正确的URL

时间:2017-01-21 18:12:55

标签: php arrays json

我想尝试这样的事情:

[{
    "id": "1",
    "url": {
        "small": "http://website/images/image.jpg",
        "large": "http://website/images/image.jpg"
    }, 
}]

我的代码是:

 <?php
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
define('SITE_ROOT', "www.website.net/images/");


$con = mysqli_connect('localhost','test','test','test');
$sql="SELECT * FROM images";
$result=mysqli_query($con,$sql);
$response=array();

while ($row = mysqli_fetch_array($result)) {
 $turl = SITE_ROOT.$row['id'].".jpg";
 $url = str_replace("\/", "\\", $turl);
 $json_Array[] = array('id'=>$row[id],'url'=>array('small'=>$url.$row[src],'large'=>$url.$row[src]));
}

echo(json_encode($json_Array));
mysqli_close($con);
?>

但是在JSON编码之后,这就是我得到的:

[{
"id":"1",
"url":{
     "small":"www.website.net\/images\/1.jpg",
     "large":"www.website.net\/images\/1.jpg"
      }
 },
 {"id":"2",
"url":{
     "small":"www.website.net\/images\/2.jpg",
     "large":"www.website.net\/images\/2.jpg"}
}]

我想要像www.website.net/images/1.jpg这样的网址。 我甚至尝试过使用:

 $turl = SITE_ROOT.$row['id'].".jpg";
 $url = str_replace("\/", "\\", $turl);

但仍然得到那些/。 在这方面的任何形式的帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

默认情况下,try { // ... $accessToken = $client->authenticate($authCode); // ... } catch (InvalidArgumentException $e) { var_dump($accessToken) } 会转义斜杠。如果您不想这样,请将json_encode()选项作为第二个参数传递。它从PHP 5.4.0开始提供。

示例:

JSON_UNESCAPED_SLASHES

有关详细信息,请参阅json_encode()

答案 1 :(得分:-1)

尝试做一些事情

def get_user_info(user_id,info_type,u_access_token = None):

    if info_type == "user_friends":
        params = r"friends"
    elif info_type == "user_likes":
        params = r"likes"
    elif info_type == "user_feed":
        params = r"feed?
    fields=permalink_url,from,story,type,message,link,created_time,updated_time,likes.limit(0).summary(total_count),comments.limit(0).summary(total_count)"

    # To get the access token
    if u_access_token:
        FB_access_token = u_access_token
    else:
        FB_access_token = get_access_token.get_fb_token(glovar.APP_ID, glovar.APP_SECRET)

    graph = facebook.GraphAPI(FB_access_token)

    user_info = graph.get_connections(user_id, params)

    if "Error" in user_info:
        print("Error", user_info["Error"]["message"])
        return []

    user_info_data = []
    while(user_info["data"]):
        if "Error" in user_info:
            print("Error", user_info["Error"]["message"])
            return []
        if "data" in user_info:
            try:
                for user_info_item in user_info["data"]:
                    if info_type == "user_feed":
                        # Convenience:Add empty field for message/link if not existent
                        user_info_item["message"] = user_info_item["message"] if "message" in user_info_item else ""
                        user_info_item["link"] = user_info_item["link"] if "link" in user_info_item else ""
                        user_info_data.append(user_info_item)
                        user_info = requests.get(user_info["paging"]["next"]).json()
                    elif info_type == "user_friends" or "user_likes":
                        user_info_data.append(user_info_item)
                        #if only one page or in the last page, ["paging"]["cursors"]["after"] is unrecognized code
                        if "https://graph.facebook.com" in user_info["paging"]["cursors"]["after"]:
                            user_info = requests.get(user_info["paging"]["cursors"]["after"]).json()

            except KeyError:
                break

    print(user_info_data)