2017-06-16 85 views
0

我想創建一個像這樣的json響應,如下所示。如何通過修改下面的php代碼來完成。我嘗試了很多方法,但結果並不存在。如何獲得這樣的json格式?

請找到鏈接編輯

https://www.froala.com/wysiwyg-editor/docs/concepts/image/manager 這是我從代碼獲得電流響應低於

[ 
    "http://cloudpanda.org//images/media/01fe5273acbd47e413b02bbcfae5a20ac868d037.jpg", 
    "http://cloudpanda.org//images/media/022fa4051066da105688a8ca0f83d222cef3739d.jpg", 
] 

期望的迴應,我從代碼需要如下:

[ 
     { 
      "url":"http://cloudpanda.org//images/media/01fe5273acbd47e413b02bbcfae5a20ac868d037.jpg", 
     }, 
     { 
      "url":"http://cloudpanda.org//images/media/022fa4051066da105688a8ca0f83d222cef3739d.jpg", 
     }, 

    ] 

當前的PHP代碼如下所示:

<?php 
// Array of image links to return. 
$response = array(); 

// Image types. 
$image_types = array(
    "image/gif", 
    "image/jpg", 
    "image/pjpeg", 
    "image/jpeg", 
    "image/pjpeg", 
    "image/png", 
    "image/x-png" 
); 

// Filenames in the uploads folder. 
$fnames = scandir("images/media/"); 

// Check if folder exists. 
if ($fnames) { 
    // Go through all the filenames in the folder. 
    foreach ($fnames as $name) { 
     // Filename must not be a folder. 
     if (!is_dir($name)) { 
      // Check if file is an image. 
      if (in_array(mime_content_type(getcwd() . "/images/media/" . $name), $image_types)) { 
       // Add to the array of links. 
       array_push($response, "http://cloudpanda.org/images/media/" . $name); 
      } 
     } 
    } 
} 

// Folder does not exist, respond with a JSON to throw error. 
else { 
    $response  = new StdClass; 
    $response->error = "Images folder does not exist!"; 
} 

$response = json_encode($response); 

// Send response. 
echo stripslashes($response); 
?> 
+0

這裏有什麼您的問題? –

+0

我需要獲取響應變量作爲上面的json代碼。當然,我只是得到一個網址列表只有我需要得到它像這樣的「網址」:「https://i.froala.com/assets/photo1.jpg」, –

+0

你的鏈接是平淡的 –

回答

1

編輯您這樣的代碼,更改

array_push($response, "http://cloudpanda.org/images/media/" . $name); 

$response[]['url'] = "http://cloudpanda.org/images/media/" . $name; 
+1

謝謝你喲它工作:) –