3 Using curl to Query Remote Servers - PHP Tutorials

Using curl to Query Remote Servers

Authenticating with curl

All popular methods of HTTP authentication are supported, including HTTP basic, digest, GSS and NTLM. As with other curl setting the curl_setopt function is used. To set the authentication method do curl_setopt(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); this would set basic authentication. Multiple authentication types can be selected via the | or selector. The credentials are set with curl_setopt(CURLOPT_USERPWD, '[username]:[password]').

The other authentication options are CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM for digest, GSS negotiate, NTLM respectively. Along with those there are also CURLAUTH_ANY which is an alias to CURLAUTH_BASIC|CURLAUTH_DIGEST|CURLAUTH_GSSNEGOTIATE|CURLAUTH_NTLM and CURLAUTH_ANYSAFE which aliases to CURLAUTH_DIGEST|CURLAUTH_GSSNEGOTIATE|CURLAUTH_NTLM.

Example 1:
<? 
$ch 
curl_init();
curl_setopt($chCURLOPT_URL'http://www.example.com');
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
curl_setopt(CURLOPT_USERPWD'[username]:[password]')

$data curl_exec();
curl_close($ch);
 
?>

Example 2:
<? 
$ch 
curl_init();
curl_setopt($chCURLOPT_URL'http://www.example.com');
curl_setopt($chCURLOPT_RETURNTRANSFER1);
curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_ANY);
curl_setopt(CURLOPT_USERPWD'[username]:[password]')

$data curl_exec();
curl_close($ch);
 
?>

In the first example we are just attempting to authenticate with basic authentication but in the second example we're going to try all possible authentication methods.

Using SSL and proxies in curl <<  1 2 3 4 5
New Content