2

curl have a CURLOPT_LOGIN_OPTIONS parameter ( --login-options in terminal app). How to use it in php script with php5-curl? By default, it has no CURLOPT_LOGIN_OPTIONS and use code like curl_setopt($ch, 12345, "auth=PLAIN"); doesn't change lead to curl behavioral change. I won't use exec in my code. Thanks.

waterlilies
  • 21
  • 1
  • 3
  • http://stackoverflow.com/questions/2140419/how-do-i-make-a-request-using-http-basic-authentication-with-php-curl – Farkie May 12 '16 at 12:58
  • Thanks for comment, @Farkie, but I need to implement this command curl --url "imaps://server.com" --user "username:password" --login-options "auth=PLAIN" – waterlilies May 12 '16 at 19:48

1 Answers1

2

In PHP if you want to login on some server you can use simple setup:

curl_setopt($cURL, CURLOPT_USERPWD, "USERNAME:PASSWORD");

Here is one my example how I use that:

$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, "imaps://server.com");
curl_setopt($cURL, CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_POSTFIELDS,
        "postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($cURL, CURLOPT_TIMEOUT, 2);
curl_setopt($cURL, CURLOPT_USERPWD, "USERNAME:PASSWORD");
curl_setopt($cURL, CURLOPT_HTTPHEADER, array("auth=PLAIN"));
$output=curl_exec($cURL);
curl_close($cURL);

CURLOPT_USERPWD allow you to login on your server or application if you setup like that.

If you need proxy, there is another way:

curl_setopt($cURL, CURLOPT_PROXYUSERPWD, "USERNAME:PASSWORD");

But you also need additional options for that:

curl_setopt($cURL, CURLOPT_PROXY, '127.0.0.1'); // IP
curl_setopt($cURL, CURLOPT_PROXYPORT, '23'); // PORT
curl_setopt($cURL, CURLOPT_PROXYUSERPWD, "USERNAME:PASSWORD"); // Login

I hope this can help.

Ivijan Stefan Stipić
  • 6,249
  • 6
  • 45
  • 78
  • curl_setopt($cURL, CURLOPT_USERPWD, "USERNAME:PASSWORD"); is like curl_setopt($ch, CURLOPT_USERNAME, username); curl_setopt($ch, CURLOPT_PASSWORD, password); I need to implement this command curl --url "imaps://server.com" --user "username:password" --login-options "auth=PLAIN" – waterlilies May 12 '16 at 19:45
  • I update my answer. You need send `CURLOPT_HTTPHEADER` with `auth=PLAIN` to get this work. I implement your request. And if you don't need send POST informations, just remove that part – Ivijan Stefan Stipić May 18 '17 at 05:21