簡體   English   中英

Symfony 1.4.2 PHP cURL PUT請求中沒有內容

[英]Symfony 1.4.2 no content in PHP cURL PUT request

使用PHP cURL和Symfony 1.4.2

我正在嘗試執行包含數據(JSON)的PUT請求,以修改我的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請求設置了以下設置:

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;

因此,我嘗試將標頭的Content-Type設置為它,但是它什么也沒做。

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

根據另一個問題/答案 ,我已經測試了此解決方案,並且得到了正確的結果:

$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數據字符串

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM