Basic authentication without CURL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erwin Moller

    Basic authentication without CURL

    Hi all,

    I want to create an URL to use with fopen($url).
    The URL needs basic authentication, but I have no CURL on this
    particular machine.
    (I need to access an app that uses REST.)

    I tried to use the URLwrappers for fopen, but to no avail.

    Samplecode:

    $protocol = "http://";
    $url = "www.example.co m/administration/webstatistics";
    $username = "bla";
    $password = "mypass";
    $authURL = $protocol.$user name.":".$passw ord."@".$url;
    $content = file_get_conten ts($authURL,'r' );
    etc.

    Warning: file_get_conten ts(): HTTP request failed! HTTP/1.1 401
    Authorization Required in <removed>/rest.php on line 16

    I know this was a shot in the dark, but that kind of URL works from my
    browser, so I thought I give it a shot. Bad luck. :-(

    Does anybody know if this is possible using simply fopen?

    I have been reading through the streampages on php.net, but that is kind
    of overwhelming to grasps right away. Working on it now.

    Any advise?

    Regards,
    Erwin Moller
  • Erwin Moller

    #2
    [solved] Re: Basic authentication without CURL

    Erwin Moller schreef:
    Hi all,
    >
    I want to create an URL to use with fopen($url).
    The URL needs basic authentication, but I have no CURL on this
    particular machine.
    (I need to access an app that uses REST.)
    >
    I tried to use the URLwrappers for fopen, but to no avail.
    >
    Samplecode:
    >
    $protocol = "http://";
    $url = "www.example.co m/administration/webstatistics";
    $username = "bla";
    $password = "mypass";
    $authURL = $protocol.$user name.":".$passw ord."@".$url;
    $content = file_get_conten ts($authURL,'r' );
    etc.
    >
    Warning: file_get_conten ts(): HTTP request failed! HTTP/1.1 401
    Authorization Required in <removed>/rest.php on line 16
    >
    I know this was a shot in the dark, but that kind of URL works from my
    browser, so I thought I give it a shot. Bad luck. :-(
    >
    Does anybody know if this is possible using simply fopen?
    >
    I have been reading through the streampages on php.net, but that is kind
    of overwhelming to grasps right away. Working on it now.
    >
    Any advise?
    >
    Regards,
    Erwin Moller
    Hi,

    Sorry for the noise, but it is solved (without curl).

    Solution was to use a context.
    Here is some samplecode in case somebody runs into the same problem.
    (It was posted on php.net under usercontributed notes.)


    $protocol = "http://";
    $url = "www.example.co m/administration/webstatistics";
    $username = "bla";
    $password = "mypass";
    $cred = sprintf('Author ization: Basic %s',
    base64_encode(" $username:$pass word") );
    $opts = array(
    'http'=>array(
    'method'=>'GET' ,
    'header'=>$cred )
    );
    $ctx = stream_context_ create($opts);
    $handle = fopen ( $protocol.$url, 'r', false,$ctx);
    // the 1000000 is not very subtle. :P
    $content = fread($handle,1 000000);


    Regards,
    Erwin Moller

    Comment

    • disccomp

      #3
      Re: Basic authentication without CURL

      // Cleaned up a bit:
      function http_auth_get($ url,$username,$ password){
      $cred = sprintf('Author ization: Basic %s',
      base64_encode(" $username:$pass word") );
      $opts = array(
      'http'=>array(
      'method'=>'GET' ,
      'header'=>$cred )
      );
      $ctx = stream_context_ create($opts);
      $handle = fopen ( $url, 'r', false,$ctx);

      return stream_get_cont ents($handle);

      }

      Comment

      • Erwin Moller

        #4
        Re: Basic authentication without CURL

        disccomp schreef:
        // Cleaned up a bit:
        function http_auth_get($ url,$username,$ password){
        $cred = sprintf('Author ization: Basic %s',
        base64_encode(" $username:$pass word") );
        $opts = array(
        'http'=>array(
        'method'=>'GET' ,
        'header'=>$cred )
        );
        $ctx = stream_context_ create($opts);
        $handle = fopen ( $url, 'r', false,$ctx);
        >
        return stream_get_cont ents($handle);
        >
        }
        A very late: thanks. :-)
        Much prettier indeed.

        Regards,
        Erwin Moller

        Comment

        Working...