沒有內容使用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;
於是,我就設置標題的內容類型,但它沒有做任何事情。
如果您有任何想法,請幫忙!
當您使用PUT HTTP方法時,'$ request-> getPostParameter()'不起作用。 'file_get_contents(「php:// input」)'似乎是正確的做法。在閱讀輸入之後,請檢查你究竟得到了什麼(我不確定'parse_str'是否可以在這裏工作)。或者,如果方法不重要,可以隨時切換到POST。 – 2013-03-14 09:21:56
我試圖只打印'file_get_contents(「php:// input」)',這是空的。我試過使用'$ request-> getPostParameter()',因爲在sfWebRequest.php中有一行將內容添加到post參數中(見上文)。 – Dachmt 2013-03-14 19:10:24