0

I need to translate this cURL command into PHP cURL code:

curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d '[email protected]&j_password=site1' "https://login.uat.site.be/openid/login.do"

I have tried this, but it doesn't seem to work, I kept getting empty as my result:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://login.uat.site.be/openid/login.do");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "[email protected]&j_password=site1");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIEJAR, public_path().'/session_cookies.txt');

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

dd($result); I kept getting empty string, however it works in the standard curl command.

4
  • For debug only, add curl_setopt($ch, CURLOPT_VERBOSE, TRUE);. Commented Jan 24, 2017 at 20:42
  • Is login.uat.site.be the real address? It says "* Could not resolve host: login.uat.site.be" for me. Commented Jan 24, 2017 at 20:43
  • @axiac : please assuming it is. I can't really throw my client URL in here. I'm so sorry for that. Commented Jan 24, 2017 at 20:45
  • Why don't you use guzzle library, which is a wrapper around curl, which lets you do this in a few lines of code? Commented Jan 24, 2017 at 22:01

2 Answers 2

1

looks like you're making these mistakes:

1: you don't enable CURLOPT_FOLLOWLOCATION. try curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

2: j_username and j_password is not properly encoded. (for instance, @ should actually be encoded as %40 ) try curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('j_username'=>'[email protected]','j_password'=>'site1')));

also note that you probably should not manually set Content-Type: application/x-www-form-urlencoded , maybe it doesn't make a difference, but i'd rather have curl/php set it automatically if i were you (practically no chance of a misplaced space or a typo etc), according to the docs: Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded. - and you are passing a string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out my mistakes. Especially the url encode one, good eyes ! Awesome answer !
1
  1. The remote site uses HTTPS protocol. So you have to set CA options: CURLOPT_CAINFO / CURLOPT_CAPATH. Alternatively, you may just turn peer's verifying off by setting CURLOPT_SSL_VERIFYPEER option to false if you don't fear man-in-the-middle attack.

  2. You haven't translated -L option. CURLOPT_FOLLOWLOCATION - its counterpart in PHP.

  3. You've translated -b option wrong. -b specifies a file to read previously stored cookie from. It also activates the cookie engine which will make curl record incoming cookies there automatically. PHP CURL has no direct equivalent of this option. CURLOPT_COOKIE only accept contents of cookie header, not a filename. CURLOPT_COOKIEJAR only specifies filename to save incoming cookies, but it doesn't pass them with requests automatically. In order to reproduce the full effect of -b option you have to use CURLOPT_COOKIEJAR in combination with CURLOPT_COOKIEFILE option which specifies a file to read cookies. Both options must be set to the same value. Also both expect absolute path to a file. If you want to store cookies in the current script's directory you may use $_SERVER['PHP_SELF'] or __FILE__ in combination with dirname() function. So public_path() function should return either a fixed absolute path or a relative path prepended with the current directory.

  4. As @hanshenrik wrote, POST data should be URL-encoded, as it supposed by the content type of request (application/x-www-form-urlencoded). The most appropriate way to do it is use http_build_query function.

  5. You should consider using curl_setopt_array function instead of bunch of curl_setopt calls.

So the code should (may) look like this:

$cookie_file = dirname(__FILE__). '/session_cookies.txt';
$options = array(
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query(array(
        'j_username' => '[email protected]',
        'j_password' => 'site1',
    )),
    CURLOPT_COOKIEJAR => $cookie_file,
    CURLOPT_COOKIEFILE => $cookie_file,
);
$ch = curl_init();
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_URL, 'https://login.uat.site.be/openid/login.do');
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

3 Comments

I can not thank-you enough for this long details answer in steps, I appreciated your time, and effort put in to this. I do understand way better now after reading your answer. Cheers !
I've tried your suggestion, and still facing some errors, can you please give me a little more hints ?
I am so sorry. I tried what you suggested and put together the details in my new post, Please have a look, maybe you can spot something that I missed. : stackoverflow.com/q/41855264/4480164

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.