5

As shown in image when i am trying to run https://www.example.com/Push_Order.php?orderId=1562 It gives me 401 Unathorized error. But when I run this Url in browser it run well.

Any Idea where is my mistake or what i am missing?

enter image description here

PHP Curl Code

<?php

$ch = curl_init("https://www.example.com/Push_Order.php?orderId=1562");

curl_setopt($ch);

$response = curl_exec($ch);

curl_close($ch);
?>

and this Push_Order.php file contains only insert query.

2
  • Sure, your mistake is that you have no code and your current question has nothing to do with php. Commented May 3, 2016 at 13:28
  • @Inurosen the file contains insert query only. Commented May 3, 2016 at 13:41

2 Answers 2

12

It appears you have authorization enabled in Apache on your server but you don't realize it at the moment because your browser is caching the username and password for you.

You have to set the CURLOPT_USERPWD option like so:

<?php

$ch = curl_init("https://www.example.com/Push_Order.php?orderId=1562");

curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");

$response = curl_exec($ch);

curl_close($ch);
?>

(The empty curl_setopt($ch) was weird and a no-op at best anyway.)

In the command line it should work just like this (including the username and password in the URL):

curl https://myusername:[email protected]/Push_Order.php?orderId=1562
Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are right.. I had placed .httaccess file.. Thanks !
0

I had exactly this error message coming from my Apache2 web server and it took me a little while to diagnose, so I want to document what was wrong for anyone researching the same problem as I had, and landing here.

I assumed incorrectly that my Apache was running fine and I really had an authorization problem, as that is what the message implies. The reality was I had syntax errors in my Apache config files.

If a local curl gives you this

enter image description here

(character for character the same, give or take a URL and port number,) you might want to check you can happily run

service apache2 reload

before investigating authorization too heavily!

Comments

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.