【问题标题】:How to curl structure如何卷曲结构
【发布时间】:2016-04-12 20:15:47
【问题描述】:

如何在我的 php 中使用 curl 从我感兴趣的 Web 应用程序中初始化此查询。

$ curl https://api.airtable.com/v0/appQmOEr6c6AV4X47/foo \
-H "Authorization: Bearer YOUR_API_KEY"

我更习惯使用 curl 之类的东西

// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
$string = print_r($json,true);
$result = json_decode($string,true);

为什么 $ 与 curl 分开,-H 是做什么的? 我在 www.stackoverflow.com 中尝试了 Google 和其他 curl 相关主题

我在这里找到了解决方案:https://incarnate.github.io/curl-to-php/ 并应用它来获取此代码,工作正常。

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.airtable.com/v0/appQmOEr6c6AV4X47/foo');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');


$headers = array();
$headers[] = 'Authorization: Bearer YOUR_API_KEY';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close ($ch);

【问题讨论】:

  • 你查阅过 curl 手册页吗?
  • curl https://api.airtable.com/v0/appQmOEr6c6AV4X47/foo \ -H "Authorization: Bearer YOUR_API_KEY" 应该从安装了 cURL 的系统的命令行调用。
  • 你为什么不用file_get_contents
  • 所以我必须在php之外做?
  • -H 选项类似于 PHP 中的 CURLOPT_POSTFIELDS 选项。

标签: php rest curl


【解决方案1】:
$ curl https://api.airtable.com/v0/appQmOEr6c6AV4X47/foo \
-H "Authorization: Bearer YOUR_API_KEY"

这是使用类 Unix (Linux) shell 运行the curl binary 的示例。 $ 是一种约定,表示您看到的是 shell 提示,并且用户是非特权用户(即不是 root)。

// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
$string = print_r($json,true);
$result = json_decode($string,true);

这是一个使用 the PHP curl library 的 PHP 代码 sn-p 示例。


这两个都使用 libcurl 一个 C 库。

【讨论】:

    【解决方案2】:
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://api.airtable.com/v0/appQmOEr6c6AV4X47/Tasks?maxRecords=3&view=Main%20View");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $headers = array();
    $headers[] = 'Authorization: Bearer YOUR_API_KEY';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $server_output = curl_exec($ch);
    curl_close ($ch);
    
    $con = json_decode($server_output);
    

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 2013-04-12
      • 2019-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      相关资源
      最近更新 更多