-1

这是我的代码 - 为什么我会得到一个空白响应?

<?php

$client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$client_id = 'xxxxxxxxxxxxxxx';

$file = file_get_contents("test-image.png");

$url = 'https://api.imgur.com/3/image.json';
$headers = array('Authorization: Client-ID $client_id');
$pvars  = array('image' => base64_encode($file));

$curl = curl_init();

curl_setopt_array($curl, array(
   CURLOPT_URL=> $url,
   CURLOPT_TIMEOUT => 30,
   CURLOPT_POST => 1,
   CURLOPT_RETURNTRANSFER => 1,
   CURLOPT_HTTPHEADER => $headers,
   CURLOPT_POSTFIELDS => $pvars
));

$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;

curl_close ($curl); 

?>

编辑:

一些 var_dump 结果:

var_dump(base64_encode($file)):
    string iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAQAAAD2e2DtAADS7ElEQVR4Xu39B7SlaVbehz9fPDndnEPl0F2dw8z05CEjGIQAC2ELhABhIbAlZMmyhOEvB0mWg2zZQsaykJElkdMwwOTUM527qit0xVs353NPjl/6r/Vb77prCcYwDNM9A5pzV9UNJ33nffe7w7OfvbeV6A++xeZfwr+UM4hs85unP/m3r97+EAGIlcjip4SffSVK+LurPw23r97cP+z820YAJFsJXzY//8m+/TUnsEciWeeS9+oF/QPJ+qvJC3pS56wXk5+3Fm1XG/FsUnJ837LDKIhdO52Sx4GJO4OZ6L5txYtBEF7Udf1k/CdeA/zpv/0lu+THtu046SStiqZUiRK1kqS1Fu62c9NzltXYq3V7g6LizPhYuuSN24/GW14qmkliHaiehNbJeCGeSBwNrY6G1lF80719b/U3N2U/kXmxrVgak5KD+KsC8GXe6HXNas9KKSfpyC44l+yB'...
(length=72080)

var_dump($json_returned):
    boolean false

编辑2:

print_r(curl_getinfo($curl)):
    '( [url] => https://api.imgur.com/3/image.json [content_type] => [http_code] => 0 [header_size] => 0 [request_size] => 0 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.296 [namelookup_time] => 0.047 [connect_time] => 0.312 [pretransfer_time] => 0 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => -1 [starttransfer_time] => 0 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 54.243.128.92 [primary_port] => 443 [local_ip] => 192.168.1.69 [local_port] => 4923 [redirect_url] => )'
4

2 回答 2

0

您需要像这样设置一个完整的标题:

$post_header  = array("POST /3/image.json HTTP/1.1",
"Host: api.imgur.com",
"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1",
"Accept-Language: en-us,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Connection: keep-alive",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
);

然后把这个header放到opt数组中CURLOPT_HTTPHEADER => $post_header

这就是 CURL POST 的工作方式。

.

此外,您需要使用http_build_query()构建要发送到的 postdata CURLOPT_POSTFIELDS,而不是直接数组。

于 2013-04-09T08:47:48.747 回答
0

我已经解决了:

'Authorization: Client-ID $client_id' should have been
"Authorization: Client-ID $client_id"

基础知识,要使用单引号,我必须连接。

于 2013-04-09T08:58:43.200 回答