I've been trying all week to try and enable connection to my web service using PHP curl, however, I couldn't make it work so I tried curl using command line and to my surprise.. it worked.
Here's the command that I used using the linux curl:
curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://<host>/appserver/j_spring_security_check -d "j_username=admin&j_password=demoserver"
How do you convert this to PHP code?
PS. I'm a newbie and have just been exposed to PHP with less than a month exp, forgive me! :D
You asked how the following linux terminal curl
command relates to the options of PHP curl:
curl -k -i -H "Content-type: application/x-www-form-urlencoded" -c cookies.txt -X POST https://192.168.100.100:444/appserver/j_spring_security_check -d "j_username=admin&j_password=demoserver"
Here is a list of the above options/flags:
- -k = CURLOPT_SSL_VERIFYPEER: false
- -i = CURLOPT_HEADER: true
- -H = CURLOPT_HTTPHEADER
- -c = CURLOPT_COOKIEJAR + CURLOPT_COOKIEFILE
- -X POST = CURLOPT_POST: true
- -d = CURLOPT_POSTFIELDS
Which would lead to exactly the following:
<?php
$ch = curl_init();
$url = "https://192.168.100.100:444/appserver/j_spring_security_check";
$postData = 'j_username=admin&j_password=demoserver';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1); // -X
curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); // -d
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'application/x-www-form-urlencoded'
)); // -H
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // -c
curl_setopt($ch, CURLOPT_HEADER, true); // -i
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // -k
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // see comment
echo curl_exec ($ch);
curl_close ($ch);
I hope this helps you.