2016-08-25 74 views
1

我把這個數據 {ID = 「1」,標題= 「富」,imagedescription = 「酒吧」}如何使用PHP rest api獲取數據?

這是我的PHP端點

<?php 

// This will update a photo that has been edited 
if (isset($_GET['id'], $_GET['title'], $_GET['imagedescription'])) 
{ 
$id= $_GET['id']; 
$title= trim($_GET['title']); 
$imagedescription=trim($_GET['imagedescription']); 

require 'db_connect.php'; 

$update_query = "Update images Set title = {$title}, imagedescription = {$imagedescription} Where id={$id}"; 

    if($update = $db->query($update_query)) 
    { 
     $response["success"] = 1; 
     $response["message"] = "Photo successfully updated."; 
     // echoing JSON response 
     echo json_encode($response); 
    } 
    else 
    { 
     $response["failed"] = 0; 
     $response["message"] = "Oops! An error occurred."; 
     $response["sql"] = $update_query; 

     // echoing JSON response 
     echo json_encode($response); 
    } 

} 
else 
{ 
// required field is missing 
$response["failed"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 

?> 

我只是得到{ 「失敗」 :0,「郵件」:「缺少必填字段」},在郵遞員中有200個成功響應。

如何使用PHP從請求中獲取數據?

這是角服務

(function() { 
angular.module('app') 
    .service('PhotosManager', ['$http', function($http) { 

      this.getPhotos = function() { 

       return $http.get("get_all_photos.php"); 
      }; 

      this.updatePhoto = function (id, data) { 
       return $http.put("update_photo.php"+ id, data); 

      }; 

    }]); 

})(); 

我可以看到一個成功的響應 請求方法:PUT 狀態代碼:200 OK

+1

的可能的複製[如何訪問在服務器端PHP REST API將數據?](http://stackoverflow.com/questions/6805570/how-do-i-access-php-rest- api-put-data-on-the-server-side) –

+0

你正在把這個數據放在這裏{{id =「1」,title =「foo」,imagedescription =「bar」}'。但從Ajax或從其他客戶端,它是GET還是POST? –

+0

$ http.put(「update_photo.php」+ id,data); – ChibaKun

回答

0

這工作。首先讀取已放置的數據,然後對其進行json_decode,最後可以從數組中訪問它。

<?php 


$putfp = fopen('php://input', 'r'); 
$putdata = ''; 
while($data = fread($putfp, 1024)) 
$putdata .= $data; 
fclose($putfp); 


$data = json_decode($putdata, true); 

// This will update a photo that has been edited 
if (isset($data['id'], $data['title'], $data['imagedescription'])) 
{ 
$id= $data['id']; 
$title= trim($data['title']); 
$imagedescription=trim($data['imagedescription']); 

require 'db_connect.php'; 

$update_query = "Update images Set title = '{$title}', imagedescription = '{$imagedescription}' Where id={$id}"; 

    if($update = $db->query($update_query)) 
    { 
     $response["success"] = 1; 
     $response["message"] = "Photo successfully updated."; 
     // echoing JSON response 
     echo json_encode($response); 
    } 
    else 
    { 
     $response["failed"] = 0; 
     $response["message"] = "Oops! An error occurred."; 
     $response["sql"] = $update_query; 

     // echoing JSON response 
     echo json_encode($response); 
    } 

} 
else 
{ 
// required field is missing 
$response["failed"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
} 

?> 
相關問題