Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过需要验证的URL访问Json数据_Php_Json - Fatal编程技术网

Php 通过需要验证的URL访问Json数据

Php 通过需要验证的URL访问Json数据,php,json,Php,Json,我似乎找不到更多关于如何通过PHP完全操作Json数据的教程或资源。看起来几乎所有的事情都可以通过一些函数等轻松完成。然而,我似乎找不到的是如何访问受密码保护且需要验证的Json URL 例如,我想访问一个链接,如,但它需要身份验证,我显然有。但不确定如何将身份验证添加到代码中 现在我有 $url ='http://armchairanalysis.com/api/1.0/game/1200/conversions'; $data = file_get_contents($url); $char

我似乎找不到更多关于如何通过PHP完全操作Json数据的教程或资源。看起来几乎所有的事情都可以通过一些函数等轻松完成。然而,我似乎找不到的是如何访问受密码保护且需要验证的Json URL

例如,我想访问一个链接,如,但它需要身份验证,我显然有。但不确定如何将身份验证添加到代码中

现在我有

$url ='http://armchairanalysis.com/api/1.0/game/1200/conversions';
$data = file_get_contents($url);
$characters = json_decode($data);

echo $characters[10]->;
代码很简单,只是不确定身份验证部分


感谢您的帮助。谢谢

此API需要HTTP基本身份验证才能访问。正确的方法是在HTTP上下文中包装
file\u get\u contents
函数

$url ='http://armchairanalysis.com/api/1.0/game/1200/conversions';

// provide your username and password here
$auth = base64_encode("username:password");

// create HTTP context with basic auth
$context = stream_context_create([
    'http' => ['header' => "Authorization: Basic $auth"]
]);

// query for data
$data = file_get_contents($url, false, $context);
$username="username";
$password="password";
$url="example.com";
$context = stream_context_create(array(
  'http' => array(
            'header'  => "Authorization: Basic " . base64_encode("$username:$password")
        )
    ));
$json = file_get_contents($url, false, $context);
print_r(json_decode($json));