I have my API Rest with Symfony (2.8.6) with FOSRestBundle and Nelmio for documentation.
All of this works perfect, on nelmio sandbox all works perfectly but when I use my API by cURL nothing happens when I try to update one resource.
$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);
In resume, I can update the "fax" field through the sandbox but nothing happens when I try to do the same with external php file in cURL
Any idea??? (the token and the URL are correct)
Thanks to all!!
Roger
EDIT
The solution (that I don't understand) is put the user agent... with this cURL all works perfectly
$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;
}
Through postman the command line is:
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"
Thanks all again!
尝试在命令行中执行curl:
curl -H 'Content-Type: application/json' -X PUT -d '{"fax":999}' http://yourhost/fr/api/users/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.