我正在尝试使用php Curl调用formstack api,但我什么都没得到

时间:2012-10-26 10:47:30

标签: php api curl formstack

这是我的第一个curls代码之一,所以它可能有错误

我正在尝试调用表单/:id /提交 https://www.formstack.com/developers/api/resources/submission#form/:id/submission_GET

如果我加载:

  

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

如果效果很好。

如果我尝试这段代码:

 <?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 个答案:

答案 0 :(得分:2)

您需要设置:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

作为curl_exec()之前的一个选项,如果您希望获取数据。

  

CURLOPT_RETURNTRANSFER:TRUE将传输作为字符串返回   返回curl_exec()的值,而不是直接输出。

另外,为什么要尝试通过GET请求发送POST数据?

  

CURLOPT_POSTFIELDS:在HTTP“POST”操作中发布的完整数据。

另外,对于调试,您应该查看:

echo curl_error ( $curl );

答案 1 :(得分:1)

这对我有用:

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