Error authenticating Json with php passing header

1

I'm having a small problem querying a URL with JSON file. Basically my code returns bool(false) .

$usuario = 'root';
$senha = '123';

$header = 'Authorization: Basic ' . base64_encode($usuario . ':' . $senha) . "\r\n";
          'Content-Type: application/json;charset=UTF-8';
opções fornecidas na options predefinição.
$context = stream_context_create(array(
    'http' => array(
                  'header' => $header
              )
    )
);
error_reporting(0);
$arquivo = "https://127.0.0.1:9088/xd/servidor/?serie=2018&?number=424";

$info = file_get_contents($arquivo);

var_dump($info);

If I instead of the url directly pass the file name, for example file.json , my var_dump() normally reads the code, however to access the URL it does not read. >

Can anyone help me?

Thank you

    
asked by anonymous 11.09.2018 / 16:26

1 answer

0

1st

Use the native PHP cURL library.

I created an example below by accessing the GitHub API that returns a JSON.

The returned JSON reports an authentication error because the user and password are incorrect. I put them here just to give an example.

See the example:

<?php
$usuario = 'root';
$senha = '123';

$url = 'https://api.github.com/?page=2';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERPWD, $usuario . ":" . $senha);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP cURL 7.29.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$retorno = curl_exec($ch);
curl_close($ch);

echo $retorno;
?>

References:

Client URL Library

Curl_init Function

2

Your URL has a further question mark on the querystring soon after "2018 &" .

This is not allowed by the HTTP protocol.

See:

https://127.0.0.1:9088/xd/servidor/?serie=2018&?number=424

change to

https://127.0.0.1:9088/xd/servidor/?serie=2018&number=424
    
15.09.2018 / 07:29