file_get_contents不显示任何数据

时间:2013-10-30 15:46:12

标签: php curl file-get-contents

我正在做

$info = file_get_contents('http://USERNAME:PASSWORD@api.appfigures.com/v2/sales?client_key=CLIENT_KEY');

$info什么都不返回。但是,当我将URL放入浏览器时,会出现JSON。我没有运气就尝试使用cURL。

$data = array('client_key' => 'CLIENT_KEY');
$link = "http://api.appfigures.com/v2/sales";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_USERPWD, "username:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$jsonData = curl_exec($ch);
curl_close($ch);

print_r($jsonData);

由于

1 个答案:

答案 0 :(得分:1)

尝试:

$url = 'http://api.appfigures.com/v2/sales?client_key=CLIENT_KEY';
$username = 'USERNAME';
$password = 'PASSWORD';
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);