PHP: cURL and htpasswd [apache auth]

After spending literally all afternoon working on it, here an example of the code I finally got to work!

curl_setopt( $this->_curlHandle, CURLOPT_URL, ‘site.com/password-protected’ );
curl_setopt( $this->_curlHandle, CURLOPT_USERPWD, ‘username:password’ );
curl_setopt( $this->_curlHandle, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $this->_curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
$result = curl_exec($this->_curlHandle);

I have bolded the part that was the culprit! I hope this saves you time [and your company money ;-) ].

Nick

Leave a Reply