2013-03-13 77 views
1

沒有內容使用PHP捲曲和Symfony的1.4.2的Symfony 1.4.2 PHP捲曲PUT請求

我試圖做一個PUT請求包括數據(JSON)修改一個對象,在我的REST Web服務,但無法捕獲服務器端的數據。

看來,內容是成功連在我的日誌檢查時:

PUT to http://localhost:8080/apiapp_test.php/v1/reports/498 with post body content=%7B%22report%22%3A%7B%22title%22%3A%22The+title+has+been+updated%22%7D%7D 

我重視這樣的數據:

$curl_opts = array(
    CURLOPT_HTTPHEADER => $headers, 
    CURLOPT_POSTFIELDS => http_build_query(array('content' => $post_data)), 
); 

,並希望用這樣的

獲取數據
$payload = $request->getPostParameter('content'); 

它不工作,我已經嘗試了很多方法來獲取這些數據在我的行爲離子文件。 我嘗試以下解決方案:

parse_str(file_get_contents("php://input"), $post_vars); 
$payload = $post_vars['content']; 
// or 
$data = $request->getContent(); // $request => sfWebRequest 
$payload = $data['content']; 
// or 
$payload = $request->getPostParameter('content'); 

// then I'd like to do that 
$json_array = json_decode($payload, true); 

我只是不知道如何讓我的行動這個數據,它是令人沮喪的,我在這裏它讀了很多話題,但沒有爲我工作。

附加信息:

我有這方面的設置爲我的捲曲要求:

curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, $http_method); 

if ($http_method === sfRequest::PUT) { 
    curl_setopt($curl_request, CURLOPT_PUT, true); 
    $content_length = array_key_exists(CURLOPT_POSTFIELDS, $curl_options) ? strlen($curl_options[CURLOPT_POSTFIELDS]) : 0; 
    $curl_options[CURLOPT_HTTPHEADER][] = 'Content-Length: ' . $content_length; 
} 

curl_setopt($curl_request, CURLOPT_URL, $url); 
curl_setopt($curl_request, CURLOPT_CONNECTTIMEOUT, 4); 
curl_setopt($curl_request, CURLOPT_TIMEOUT, 4); 
curl_setopt($curl_request, CURLOPT_DNS_CACHE_TIMEOUT, 0); 
curl_setopt($curl_request, CURLOPT_NOSIGNAL, true); 
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, true); 

在sfWebRequest.php,我已經看到了這一點:

case 'PUT': 
    $this->setMethod(self::PUT); 
    if ('application/x-www-form-urlencoded' === $this->getContentType()) 
    { 
    parse_str($this->getContent(), $postParameters); 
    } 
    break; 

於是,我就設置標題的內容類型,但它沒有做任何事情。

如果您有任何想法,請幫忙!

+0

當您使用PUT HTTP方法時,'$ request-> getPostParameter()'不起作用。 'file_get_contents(「php:// input」)'似乎是正確的做法。在閱讀輸入之後,請檢查你究竟得到了什麼(我不確定'parse_str'是否可以在這裏工作)。或者,如果方法不重要,可以隨時切換到POST。 – 2013-03-14 09:21:56

+0

我試圖只打印'file_get_contents(「php:// input」)',這是空的。我試過使用'$ request-> getPostParameter()',因爲在sfWebRequest.php中有一行將內容添加到post參數中(見上文)。 – Dachmt 2013-03-14 19:10:24

回答

2

an other question/answer,我測試過這個解決方案,我得到了正確的結果:

$body = 'the RAW data string I want to send'; 

/** use a max of 256KB of RAM before going to disk */ 
$fp = fopen('php://temp/maxmemory:256000', 'w'); 
if (!$fp) { 
    die('could not open temp memory data'); 
} 
fwrite($fp, $body); 
fseek($fp, 0); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_PUT, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body)); 

$output = curl_exec($ch); 

echo $output; 
die(); 

而在另一邊,則可以使用檢索內容:

$content = $request->getContent(); 

如果您var_dump它,你會檢索:

我想發送的RAW數據字符串

+0

感謝您的幫助! '$ output'包含原始JSON文件,我想在另一邊獲得。但是,'$ content = $ request-> getContent();'不檢索內容,它在那裏是空的。 – Dachmt 2013-03-14 19:03:13

+0

如果'$ output'包含原始JSON,那是因爲對方var_dump它。你看? – j0k 2013-03-14 22:29:55

+0

這正是我的想法,如果我得到了與內容的迴應,這意味着它在請求中的某處(但無法得到它)。挖掘它... – Dachmt 2013-03-14 22:45:03