0

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);
  • 1
    Is there any reason you could not use drupal_http_request()? – kiamlaluno May 20 '11 at 23:49
  • 1
    How exactly is this code being executed by Drupal? Is it in a hook implementation somewhere, and if so, which hook? Or is it being passed through the PHP filter maybe? – Garrett Albright May 21 '11 at 6:55
  • @garrett-albright its just simple a file that is being included. I've attempted included it outside of a function, in a function, in the index.php file, before and after the bootstrap. It makes no difference. If it runs after bootstrap, the problem occurs. – lordg May 21 '11 at 9:09
  • 1
    @kiamlaluno I have several other pieces of code and api classes written using CURL, as it gives the maximum control. They are classes that I may run outside Drupal in the future, so I cannot rely on a drupal function. – lordg May 21 '11 at 9:11
  • 1
    Here's a Drupal documentation page I wrote that demonstrates communicating with a remote API. The method shown has two implementations, one with cURL and one with the Drupal API. you can use that as a guide to see what you're doing different from my example. I've found cURL to have no limitations between being used inside or outside of a Drupal run-time environment... drupal.org/node/1070066 – Blake Senftner May 21 '11 at 14:28
1

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.