【问题标题】:How to use Hub API using Curl in php如何在 php 中使用 Curl 使用 Hub API
【发布时间】:2017-11-17 10:39:26
【问题描述】:

我从 RouteHappy 网站购买了 Hub API,用于使用 Source 和 Destination 搜索航班。

他们给了我下面的代码来使用他们的 API

curl \
  -H "Auth: YOUR_API_KEY" \
  -H "Accept: application/vnd.api.v3+json" \
  -G \
  -d ids=DSM-ORD-AA-3558-20160907-FIRST \
  -d include=seat,wifi \
  YOUR_HOST/segments

但我无法在 PHP、Laravel 中使用 Curl 使用 Flight API。请帮助我访问 API 并获得所需的结果。

【问题讨论】:

  • 你试过什么,你的代码在哪里?请阅读:stackoverflow.com/help/how-to-ask
  • 这个curl请求是怎么用ph​​p写的?
  • 我不知道这一点。你能指导我如何在 php 代码中使用它吗?
  • 您可能应该考虑使用 Guzzle。 github.com/guzzle/guzzle
  • 好的,谢谢,但是我可以在哪里传递 API 密钥

标签: php api curl laravel-5


【解决方案1】:

使用hhb_curl 的等效项是

echo (new hhb_curl ( '', true ))->setopt_array ( array (
        CURLOPT_URL => 'YOUR_HOST/segments',
        CURLOPT_HTTPHEADER => array (
                "Auth: YOUR_API_KEY",
                "Accept: application/vnd.api.v3+json" 
        ),
        CURLOPT_POSTFIELDS => http_build_query ( array (
                'ids' => 'DSM-ORD-AA-3558-20160907-FIRST',
                'include' => 'seat,wifi' 
        ) ) 
) )->exec ()->getStdOut ();
  • 和使用 the native curl_ api 的等价物将非常相似,但需要更多的体力劳动(检查 setopt 和 exec 的返回类型,并为 curl 创建一个临时输出文件,清理资源/内存完成后的临时文件和 curl 等,所有这些 hhb_curl 都会自动为您完成。并且 curl 错误将转换为 RuntimeExceptions)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多