0
我需要在一個Symfony的3應用程序(服務器1),其具有發送服務器2上的(過濾後)請求的路由,然後發送回服務器2(相同的HTTP狀態代碼,標題給出的精確響應與身體)。如何轉發到SF響應對象的捲曲外部原始響應?
隨着本地curl
PHP庫,你可以得到原料響應(包括標題),由CURLOPT_HEADER
選項設置爲True
。
但是從Symfony的HttpFoundation
的Response
對象似乎只有通過單獨設置的頭文件(在構造函數中,或$response->headers->set()
)和體(具有$response->setContent()
配置的。我沒有找到一種方法來設置一個原料響應( 。用頭)到Response
對象
是否有可能,或怎麼可能可以做,否則
這裏是我的嘗試:?
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MyController extends Controller
{
/**
* @Route("/get", name="get")
*/
public function getAction(Request $request)
{
// Filter/modify the query string, but keep it quite similar:
$request->query->remove('some_private_attr');
$my_query_string = http_build_query($request->query->all());
// Setup the curl request:
$curl = curl_init('http://localhost?'.$my_query_string);
curl_setopt($curl, CURLOPT_HEADER, 1); // Include headers
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // Return data as a string
curl_setopt($curl, CURLOPT_PORT, 8200);
// Perform the request, returning the raw response
// (headers included) as a string:
$result = curl_exec($curl);
// Get the response status code:
$status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// Here, how can I pass the raw external response ($result)
// to a new Response object, without parsing the header
// and body parts unnecessarily?
// Of course, the following doesn't send the right headers:
$response = new Response($result, $status_code);
return $response;
}
}
我已經知道了,問題是,我想集全響應(頭+內容)在一次操作中,因爲我與已經包含頭文件的響應(所以沒有必要「解析」回來,並向前)。不過說實在的我忘了提'$ resp->包頭中>設置()'(編輯問題) – yolenoyer