为什么stream_context_create成功返回数据而不是我的Curl?

When I first started, I thought Curl would be an excellent way of retrieving a chunk of data in the format json. It didn't work. I tried doing some Ajax request instead, but that didn't work either.

Now, this is my Curl request:

$ch = curl_init("url");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept' => 'application/json',
    'Auth' => 'code',
));

$data = curl_exec($ch);

curl_close($ch);

print_r($data);

... The CURL requests RETURNS a EMPTY STRING. No errors...

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept: application/json
" . "Auth: code",
  )
);

$context = stream_context_create($opts);



$url = "";

$fp = fopen($url, 'r', false, $context);

$r = @stream_get_contents($fp);

fclose($fp);

print_r($r);

Provides a nice array with json data. Why? Isn't this literally supposed to do the same thing?

Because CURLOPT_HTTPHEADER doesn't take associated arrays. You need to add the complete header.

$ch = curl_init("url");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Accept: application/json',
    'Auth: code',
));

$data = curl_exec($ch);

curl_close($ch);

print_r($data);