我目前正在使用gifs.com API和php使用CUrl,但我得到的回复并不是纯粹的JSON
以下代码是我从curl_exec()
HTTP/1.1 200 OK Server: nginx Date: Thu, 02 Nov 2017 16:34:48 GMT Content-
Type: application/json Content-Length: 472 Access-Control-Allow-Credentials:
false Access-Control-Allow-Headers: Origin, Accept,Content-Type,Gifs-API-Key
Access-Control-Allow-Methods: GET,POST,OPTIONS Access-Control-Allow-Origin:
* Access-Control-Max-Age: 43200 Request-Id: 942c0be8-0613-4c43-8fc0-
fbedf62e7008 Via: 1.1 google Alt-Svc: clear
{ "success":
{ "page": "https://gifs.com/gif/9QQDvZ",
"oembed": "https://gifs.com/oembed/9QQDvZ",
"embed": "<iframe src='https://gifs.com/embed/9QQDvZ' frameborder='0' scrolling='no' width='377' height='377' style='-webkit-backface-visibility: hidden;-webkit-transform: scale(1);' ></iframe>",
"files": {
"gif": "https://j.gifs.com/9QQDvZ.gif",
"jpg": "https://j.gifs.com/9QQDvZ.jpg",
"mp4": "https://j.gifs.com/9QQDvZ.mp4" }
}
}
如果我在响应中使用它, json_decode()
将返回null,我尝试substr()
删除多余的字符,但由于字符数量会不断变化,因此不会保持一致
修改
下面的是卷曲配置
$url = 'https://api.gifs.com/media/import';
$headers = array("Gifs-API-Key: willnotbedisplayed", "Content-Type: application/json"); // cURL headers for file uploading
$postfields = "{\n \"source\": \"willnotbedisplayed\",\n \"title\": \"guineapig\",\n \"tags\": [\"crazy\", \"hand drawn\", \"2015\", \"art\"],\n \"attribution\": {\n \"site\": \"vine\",\n \"user\": \"someone\"\n }\n}";
$ch = curl_init();
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_RETURNTRANSFER => true
); // cURL options
curl_setopt_array($ch, $options);
$server_output = curl_exec($ch);
答案 0 :(得分:0)
因为您使用过:
CURLOPT_HEADER => true,
告诉curl
在输出中包含HTTP标头。
摆脱它。