1

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

Mark Toledo
  • 101
  • 1
  • 15
  • 1
    Possible duplicate of [Convert command line cURL to PHP cURL](http://stackoverflow.com/questions/1939609/convert-command-line-curl-to-php-curl) – gaurav Feb 19 '17 at 17:01

1 Answers1

1

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_chec‌​k -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_chec‌​k";
    $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.

mevdschee
  • 1,625
  • 19
  • 16
  • Thanks @mevdshchee, you provide tons of help! I'm almost there.. Do you have any idea why PHP curl doesn't push through like the linux curl? Here's the result when i execute linux curl. `HTTP/1.1 302 Found Date: Thu, 16 Feb 2017 19:12:22 GMT Server: Apache/2.2.26 (Unix) mod_ssl/2.2.25 OpenSSL/1.0.1e mod_jk/1.2.37 Set-Cookie: JSESSIONID=9D1761E49919B0FCE8E077E70E250749; Path=/appserver/; HttpOnly Location: https://192.168.100.100:444/appserver/portal/welcome;jsessionid=9D1761E49919B0FCE8E077E70E250749 Content-Length: 0 Access-Control-Allow-Origin: * Content-Type: text/plain` – Mark Toledo Feb 20 '17 at 07:17
  • You probably need to add a 'CURLOPT_FOLLOWLOCATION' option to follow redirects. I added this to the code. – mevdschee Feb 20 '17 at 23:28