curl 401 Unauthorized error

41,523

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
Share:
41,523
Jaydeep Pandya
Author by

Jaydeep Pandya

Updated on October 24, 2020

Comments

  • Jaydeep Pandya
    Jaydeep Pandya over 3 years

    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.

  • Confused
    Confused over 5 years
    Yes you are right.. I had placed .httaccess file.. Thanks !