简体   繁体   中英

How to use CURLOPT_LOGIN_OPTIONS in php-curl?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM