I'm trying to make a php Curl call to formstack api, but I get nothing

1.2k views Asked by At

This is one of my first curls code, so it can have mistakes

I'm trying to be able to make calls to form/:id/submissions https://www.formstack.com/developers/api/resources/submission#form/:id/submission_GET

If I load:

https://www.formstack.com/api/v2/form/1311091/submission.json?oauth_token=abc&min_time=2012-09-01%2000:01:01&max_time=2012-10-27%2000:01:01

If works great.

If I try this code:

 <?php    
 $host = 'https://www.formstack.com/api/v2/';

  // TODO this should manage dinamics values or build an action in every method. 
  $action = 'form/1311091/submission.json';

  $url = $host . $action;

  // TODO this values will arrive like an array with values
    $postData['oauth_token']= 'abc';
    $postData['min_time'] ='2012-09-01 00:01:01';
    $postData['max_time'] ='2012-10-27 00:01:01';

// TODO make a method with this action
function getElements($postData)
{

    $elements = array();  
    foreach ($postData as $name=>$value) {  
       $elements[] = "{$name}=".urlencode($value);  
    } 
}


  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HTTPGET, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $elements);
  $result =  curl_exec($curl) ;
  curl_close($curl);
  var_dump($result);
?>
2

There are 2 answers

1
Wayne Whitty On BEST ANSWER

You'll need to set:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

As an option before curl_exec() if you wish to get data back.

CURLOPT_RETURNTRANSFER: TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Also, why are you trying to send POST data via a GET request?

CURLOPT_POSTFIELDS: The full data to post in a HTTP "POST" operation.

Also, for debugging, you should check out:

echo curl_error ( $curl );
1
Brandon Peters On

This works for me:

<?php
$host = 'https://www.formstack.com/api/v2/';
$action = 'form/1311091/submission.json';

$url = $host . $action;

$postData = array();
$postData['oauth_token']= 'REPLACE_WITH_TOKEN';
$postData['min_time'] ='2012-09-01 00:01:01';
$postData['max_time'] ='2012-10-27 00:01:01';

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$result =  curl_exec($curl);
curl_close($curl);

var_dump($result);
?>