【问题标题】:Option -H in curl request with PHP使用 PHP 的 curl 请求中的选项 -H
【发布时间】:2017-11-06 13:39:45
【问题描述】:

我必须为我的网站使用 PHP 向 API 发出 curl 请求。

我的卷曲请求 =

curl -H "Content-Type: application/json" -H "Accept: application/json" -X GET -basic -u app:app "http://thewebsite.com"

我的问题是:如何在 PHP 的 curl_exec() 函数中翻译这个请求的 -H 选项?

我看到了不同的选项,例如“curl_setopt”,但我不知道正确的选项是什么。

所以回顾一下,“Content-Type: application/json Accept: application/json”和“-basic -u app:app”哪个选项?

谢谢:)

弗洛

【问题讨论】:

标签: php json curl


【解决方案1】:

curl 中的-H 表示请求的标头,因此您的 curl 将转换为 php,如:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://thewebsite.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

curl_setopt($ch, CURLOPT_USERPWD, "app:app");

$headers = 
    [
        "Content-Type: application/json",
        "Accept: application/json"
    ];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

curl_close($ch);

【讨论】:

  • 我有另一个选项可以在 curl 命令中翻译:"...thewebsite.com" -d "{"query":{"match_all":{"code":"xxx"}} }" 你知道怎么做吗?
  • @FlorianB 这是什么?
  • 你为什么要去掉接受勾号?当您最初的问题已得到回答时,您不应在提出新问题时这样做。
  • 只需添加一个新的 setopt curl_setopt($ch, CURLOPT_POSTFIELDS, '{"query":{"match_all":{"code":"xxx"}}}');
  • 感谢您的快速回答! ;)
猜你喜欢
  • 2019-03-07
  • 2014-11-16
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
相关资源
最近更新 更多