http_post_data basic authentication?

Posted by kristian nissen on Stack Overflow See other posts from Stack Overflow or by kristian nissen
Published on 2010-04-18T17:57:24Z Indexed on 2010/04/18 18:03 UTC
Read the original article Hit count: 301

Filed under:

I have a remote service that I need to access, according to the documentation it's restricted using basic authentication and all requests have to be posted (HTTP POST).

The documentation contains this code example - VB script:

Private Function SendRequest(ByVal Url, ByVal Username, ByVal Password, ByVal Request)
    Dim XmlHttp

    Set XmlHttp = CreateObject("MSXML2.XmlHttp")
    XmlHttp.Open "POST", Url, False, Username, Password
    XmlHttp.SetRequestHeader "Content-Type", "text/xml"
    XmlHttp.Send Request
    Set SendRequest = XmlHttp
End Function

how can I accomplish this in PHP? When I post data to the remote server it replies: 401 Unauthorized Access which is fine because I'm not posting my user/pass just the data.

Bu when I add my user/pass as it's describe here: http://dk.php.net/manual/en/http.request.options.php like this:

$res = http_post_data('https://example.com',
          $data,
          array(
            'Content-Type: "text/xml"',
            'httpauth' => base64_encode('user:pass'),
            'httpauthtype' => HTTP_AUTH_BASIC
          )
        );

the protocol is https - I get a runtime error in return (it's a .Net service). I have tried it without the base64_encode but with the same result.

© Stack Overflow or respective owner

Related posts about php