Symfony + FOSRestBundle(PUT方法中的postfields)

时间:2016-05-18 09:43:37

标签: api symfony curl put fosrestbundle

我将我的API Rest与Symfony(2.8.6)一起用FOSRestBundle和Nelmio作为文档。

所有这一切都很完美,在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"

再次感谢所有人!

1 个答案:

答案 0 :(得分:0)

尝试在命令行中执行curl:

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