PHP Curl' GET'返回NULL

时间:2018-03-31 15:22:55

标签: php curl get

首先......先发制人......是的,我之前在PHP中编写了简单的Curl请求并设法将预期数据恢复...其次是有很多类似的问题,但我仍然不能让这个工作!

以下是代码:

<?php

//Set Variables
$url = "https://coinroom.com/api/availableCurrencies";
$headers = array('accept: application/json');

//Initialise Session
$cSession = curl_init(); 

//Apply Variables
curl_setopt($cSession,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,1);
curl_setopt($cSession,CURLOPT_HEADER,$headers); 

//Execute
$result=curl_exec($cSession);
$result = json_decode($result, true);

//Close Curl
curl_close($cSession);

//Var Dump
var_dump($result);


?>

这将返回NULL,但它应返回的内容显示在此处:

https://coinroom.com/public-api

我的代码实际上有什么问题,或者api可能有些奇怪吗?

2 个答案:

答案 0 :(得分:0)

不要在Response上显示标题,以便json解码结果

<?php

//Set Variables
$url = "https://coinroom.com/api/availableCurrencies";
$headers = array('accept: application/json');

//Initialise Session
$cSession = curl_init(); 

//Apply Variables
curl_setopt($cSession,CURLOPT_URL,$url);
curl_setopt($cSession, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,1);


//Execute
$result=curl_exec($cSession);
$result = json_decode($result, true);

//Close Curl
curl_close($cSession);

//Var Dump
var_dump($result);


?>

答案 1 :(得分:0)

您只对第$headers = array('accept: application/json');行进行评论,并将第$ch行中的curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");更改为$cSession