I want to protect my domain with a password because I just use it to test scripts, but I need to allow curl HTTP POST request. Is this possible?

current .htaccess:

#AuthUserFile /home/domain/.htpasswds/.htpasswd
#AuthType Basic
#AuthName "Protected Domain"
#Require valid-user

PHP curl request

    $handle = curl_init('http://wwww.domain.com/cgi/mailScript.php');
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $serial);
    curl_exec($handle);

PHP error:

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request.

Edit: I'm not concerned about security as much as just preventing people from exploring the site.

有帮助吗?

解决方案

$username = 'login';
$password = 'pass';
//...
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($handle, CURLOPT_USERPWD, $username . ":" . $password);
curl_exec($handle);

其他提示

Here is a example function of how to achieve this.

 function curl_bounce_server( $url, $param )
{
    try
    {
        $ch = curl_init();

        $username = "your_user";
        $password = "your_pass";

        // set URL and other appropriate options
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_POST,           1 );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );

        curl_setopt( $ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );

        curl_setopt( $ch, CURLOPT_POSTFIELDS, 'json='. $param ); 

        $response = curl_exec( $ch );

        curl_close($ch);

        return $response;   
    }
    catch ( Exception $exception )
    {
       return "An exception occurred when executing the server call - exception:" . $exception->getMessage();
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top