1

I want to post data to the url using basic authentication as per RFC2617

$header = base64_encode("[email protected]:india123");

 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: text/plain', 'Authorization: Basic '.$header));

What else I need to do here, I am passing ':' separated username and password using base64 encoding?

I am getting 401 response, that is unauthorized where as my username and password are valid, I am able to login using them.

Thanks

1 Answer 1

2

Instead of manually injecting the Authentication header, use CURL_USERPWD and the CURLOPT_HTTPAUTH=>CURLAUTH_BASIC setting.

http://php.net/manual/en/function.curl-setopt.php#98164

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @mario, I used the same code, i am still getting the same error status 401
$ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ; curl_setopt($ch, CURLOPT_USERPWD, "[email protected]:india123"); curl_setopt($ch, CURLOPT_SSLVERSION,3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_NOBODY, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); curl_setopt($ch, CURLOPT_URL, $submit_url);
@devel: You should leave out the SSL stuff, try just the first two lines. Otherwise use a network sniffer to debug what cURL actually sent. (Might be an outdated version.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.