I need some help translating the following shell scripts calls to PHP
curl -3 --cookie /tmp/cookie --cookie-jar /tmp/cookie --insecure --data "login=login" --data "username=admin" --data "password=password" URL-HERE/login
curl -3 --cookie /tmp/cookie --cookie-jar /tmp/cookie --insecure --data "json={'cmd':'get-data', 'id':'1'}" URL-HERE/api
I’m having some difficulties with the first curl command.
Got as far as:
$curl=curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER ,1 );
curl_setopt($curl,CURLOPT_HEADER , 1);
curl_setopt($curl,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl,CURLOPT_COOKIEJAR, "/tmp/cookie");
curl_setopt($curl,CURLOPT_COOKIEFILE, "/tmp/cookie");
curl_setopt($curl,CURLOPT_TIMEOUT,10000);
curl_setopt($curl,CURLOPT_URL,"URL-HERE/login");
curl_setopt($curl,CURLOPT_POST ,1);
$post = array(
"username" => 'admin',
"password" => 'password',
"login" => 'login',
);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
echo curl_exec($curl);
Which seems to always render the the login form from URL-HERE/login, and subsequent JSON requests using the above cookie seem to fail with error message “need to be logged in before attempting these requests”. However, the two shell command above work flawlessly.
Am I correct in thinking –data parameters become post vars. and JSON data is a post field named “json” with contents that’s been returned by json_encode()?
You could try this: (I rewrote some of my code, NOT tested)