D
D
Dmitry2013-12-14 02:02:43
PHP
Dmitry, 2013-12-14 02:02:43

How to send data for authorization via HTTP Basic Authorization through a script?

It is necessary to authorize on the server using HTTP Basic Authorization using a PHP script .
So far I've only found this:

<?
//выделяем переменные, просто для удобства;
$login = 'login';
$pass = 'password';
$url  = 'url';
$path = '/';
//устанавливаем соединение с сервером;
$sock  = @fsockopen($url, 80, $errno, $errstr, 30);
  if (!$sock) exit("Не смог соединиться");
    $lenght  = strlen($data);  //длина данных;
    $auth =  base64_encode("{$user}:{$pass}"); //данные для аутентификации;
    $request = <<<request
POST http://{$url} HTTP/1.0
Host:{$url}
Authorization: Basic {$auth}
Content-Type:text/xml
Content-length: {$lenght}
request;
    $request .= $data;
    $answ = '';
    if(fwrite($sock,$request))
        while(!feof($sock))
            $answ.= fgets($sock);
    fclose($sock);
header("Content-type: text/plain");
print($answ);
?>

It does not work correctly, returns me this:
HTTP/1.1 408 Request Time-out
Date: Fri, 13 Dec 2013 22:40:08 GMT
Server: Apache/2.2.16 (Debian)
Vary: Accept-Encoding
Content-Length: 306
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Time-out</title>
</head><body>
<h1>Request Time-out</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
<hr>
<address>Apache/2.2.16 (Debian) Server at servername.ru Port 80</address>
</body></html>

Can anyone point me where to go to read? Or what to fix in the script? I've been struggling for about an hour now and I can't come to a solution. Or show an example of a script, with comments, if you don't mind. Thanks in advance.
PS: Strongly do not beat for stupid questions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey, 2013-12-14
@another_dream

curl --user name:password www.example.com
www.php.net/manual/en/book.curl.php

$curl = curl_init('http://www.example.com');
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "name:password");
curl_exec($curl);

A
Andrey Kroshkin, 2016-06-22
@andrium

username:[email protected][:port][path]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question