I'm new in this of curl and I try to convert this command:
curl -k -v -u "user:password" -X POST
-d'{"username":"testuser","password":"testpassword"}' -H 'Content-Type:application/json'
url
from cmd to php and i did this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("username"=>"testuser","password"=>"testpassword"));
curl_setopt($ch, CURLOPT_URL, "url");
$x= curl_exec($ch);
echo $x;
var_dump($x);
curl_close($ch);
but the web service not return nothing(it's supposed to have return me json),whats wrong? pd: i try to connect to a fortiauthenticator and sorry by mi bad english
update
hi, everybody, thanks MonkeyZeus and hindmost , for your help, this is the code correct
$ch = curl_init();
$json = json_encode(array("username"=>"testuser2","password"=>"testpassword"));//name and pass of user to create
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");//name and pass of webservices
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, "https://ip_fortiauthenticator/api/version/localusers/");
$x = curl_exec($ch);
echo $x;
var_dump($x);
echo curl_error($ch);
curl_close($ch);
i hope somebody who fight with the FortiAuthenticator can be help this code, it's for a FA version 3
When you set CURLOPT_POSTFIELDS
option with an array, CURL will force "Content-Type"
header to "multipart/form-data"
. But you need to pass POST data as JSON. So you have to set CURLOPT_POSTFIELDS
option to JSON-encoded string. Somehow like this:
$json = json_encode(array("username"=>"testuser","password"=>"testpassword"));
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_URL, "$url");
$x = curl_exec($ch);
echo $x;
var_dump($x);
curl_close($ch);
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.