簡體   English   中英

Symfony + FOSRestBundle(PUT方法中的后字段)

[英]Symfony + FOSRestBundle (postfields in PUT method)

我使用Symfony(2.8.6)和FOSRestBundle和Nelmio進行API休息以獲取文檔。

所有這些都可以完美地工作,在nelmio沙箱上都可以完美地工作,但是當我通過cURL使用我的API時,當我嘗試更新一個資源時沒有任何反應。

$curl = curl_init();
$data = array('fax' => 999);

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://".$host."/fr/api/users/1",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS, http_build_query($data),
    CURLOPT_HTTPHEADER => array(
        'cache-control: no-cache',
        'x-auth-token: ' . $token,
        'Content-Length: ' . strlen($data)
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

在簡歷中,我可以通過沙箱更新“傳真”字段,但是當我嘗試對cURL中的外部php文件執行相同操作時,沒有任何反應

任何想法??? (令牌和URL正確)

謝謝大家!!

羅傑

編輯

解決方案(我不明白)是放置用戶代理...使用此cURL都可以正常工作

$curl = curl_init();

$data_curl = http_build_query(array('fax' => 123456789));

curl_setopt_array($curl, array(
    CURLOPT_URL => "http://".$host."/fr/api/users/1",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
    CURLOPT_CUSTOMREQUEST => "PUT",
    CURLOPT_POSTFIELDS => $data_curl,
    CURLOPT_HTTPHEADER => array(
        "cache-control: no-cache",
        'Content-Length: ' . strlen($data_curl),
        "x-auth-token: $token"
    ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

通過郵遞員,命令行為:

curl -X PUT -H "x-auth-token: 112233" -H "Cache-Control: no-cache" -H "Postman-Token: fa3ba9da-986e-4c66-f03a-1d7fe1842a8b" -H "Content-Type: application/x-www-form-urlencoded" -d 'fax=55' "http://".$host."/fr/api/users/1"

再次感謝大家!

嘗試在命令行中執行curl:

curl -H 'Content-Type: application/json' -X PUT -d '{"fax":999}' http://yourhost/fr/api/users/1

暫無
暫無

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

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