开发者

Problem with cURL

开发者 https://www.devze.com 2023-03-08 17:33 出处:网络
I\'ve run into a strange issue. I have a few simple lines CURL code for calling an api service. This code runs perfectly fine when running outisde of Drupal (in php file in browser and cli), but whe

I've run into a strange issue.

I have a few simple lines CURL code for calling an api service. This code runs perfectly fine when running outisde of Drupal (in php file in browser and cli), but when the file is included within Drupal (after the bootstrap), it operates deferentially.

Under normal condition, the result returned by the API service has many results, but when run within Drupal, it only returns one result.

I suspect Drupal is changing a setting that CURL is using, which is changing how the API is understanding the call.

Does anyone know what the problem might be caused by?

The code below is derived from our api class files written within a common library. We plan to use these in the future in other PHP projects.

Here is the code:

$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);

UPDATE 1: output from CURLOPT_VERBOSE and curl_getinfo

CLI Version:

* 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] => tex开发者_开发问答t/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 Version:

* 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
)


The answer was that Drupal changes the "&" in a querystring to "&" and the remote API service only processed the argument separator "&" and not "&". So naturally when it split the post data up, it got only the first parameter in the array correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消