我遇到了一个奇怪的问题。
我有一些用于调用api服务的简单行CURL代码。当运行Drupal outsde时(浏览器和cli中的php文件),此代码运行完全正常,但是当文件包含在Drupal中时(在引导程序之后),它会以不同方式运行。
在正常情况下,API服务返回的结果有很多结果,但在Drupal中运行时,它只返回一个结果。
我怀疑Drupal正在改变CURL正在使用的设置,这正在改变API理解呼叫的方式。
有谁知道问题可能是由什么造成的?
下面的代码源自我们在公共库中编写的api类文件。我们计划将来在其他PHP项目中使用这些。
以下是代码:
$params = array(
'domain' => array(
'www.domain1.com',
'www.domain2.info',
'www.domain3.in.th',
'www.domain4.com',
'www.domain5.in',
)
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://api.postrank.com/v2/domain/activity?appkey=123456&format=json',
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params)
));
$response = curl_exec($ch);
$err = curl_errno($ch);
curl_close($ch);
print_r($response);
更新1: CURLOPT_VERBOSE和curl_getinfo
的输出CLI版本:
* About to connect() to api.postrank.com port 80 (#0) * Trying 75.101.138.238... * connected * Connected to api.postrank.com (75.101.138.238) port 80 (#0) > POST /v2/domain/activity?appkey=123456&format=json HTTP/1.1 Host: api.postrank.com Accept: */* Content-Length: 159 Content-Type: application/x-www-form-urlencoded http://api.postrank.com/v2/domain/activity?appkey=123456&format=json [content_type] => text/javascript [http_code] => 200 [header_size] => 172 [request_size] => 355 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.968 [namelookup_time] => 0 [connect_time] => 0.437 [pretransfer_time] => 0.437 [size_upload] => 159 [size_download] => 998 [speed_download] => 1030 [speed_upload] => 164 [download_content_length] => 998 [upload_content_length] => 0 [starttransfer_time] => 0.968 [redirect_time] => 0 )
Drupal版本:
* About to connect() to api.postrank.com port 80 (#0) * Trying 75.101.138.238... * connected * Connected to api.postrank.com (75.101.138.238) port 80 (#0) > POST /v2/domain/activity?appkey=123456&format=json HTTP/1.1 Host: api.postrank.com Accept: */* Content-Length: 175 Content-Type: application/x-www-form-urlencoded http://api.postrank.com/v2/domain/activity?appkey=123456&format=json [content_type] => text/javascript [http_code] => 200 [header_size] => 172 [request_size] => 371 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.905 [namelookup_time] => 0 [connect_time] => 0.437 [pretransfer_time] => 0.437 [size_upload] => 175 [size_download] => 126 [speed_download] => 139 [speed_upload] => 193 [download_content_length] => 126 [upload_content_length] => 0 [starttransfer_time] => 0.905 [redirect_time] => 0 )
答案 0 :(得分:1)
答案是Drupal改变了“&”在“&
”的查询字符串中,远程API服务只处理参数分隔符“&”而不是“&
”。因此,当它将后期数据分开时,它只能正确获得数组中的第一个参数。