【问题标题】:Trying to make a cURL request in PHP with ---location parameter [duplicate]尝试使用 ---location 参数在 PHP 中发出 cURL 请求 [重复]
【发布时间】:2020-05-08 05:10:26
【问题描述】:

我想在 PHP 中对以下请求进行 cURL:

curl --location --request GET 'xxx.xxxxxad.com/api/rest/issues?project_id=4' -H 'Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'

我实现的代码是:

<?php  

$url = "xxx.xxxxxad.com/api/rest/issues?project_id=4";  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, array('Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'));  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  

$output = curl_exec($ch);  

echo $output;

curl_close($ch);  

?>

我找不到在请求中包含 --location 的方法,否则无法获取数据。

【问题讨论】:

  • 你的问题不够清楚。请检查您使用的第一句话。
  • 阅读--location 的作用,然后阅读 PHP curl_setopt 的手册页,并尝试找出等价物是什么……?你可能想要CURLOPT_FOLLOWLOCATION
  • 也去改变你的访问密钥,因为对于那些真的想要或者只是真的无聊的人来说应该不难找出您正在使用的域,现在他们有了您的访问密钥。 (编辑你的帖子是不够的 - 所以有编辑历史)

标签: php curl php-curl


【解决方案1】:

--request GET 大致翻译为 CURLOPT_HTTPGET=&gt;1(实际上严格的翻译是 CURLOPT_CUSTOMREQUEST,但不要这样做,如果/当您尝试重新使用 curl 句柄时,这通常是一个坏主意,会导致错误对于将来的其他内容并忘记重置它 - CUSTOMREQUEST 很容易出错,根据经验:尽可能避免它。)

CURLOPT_HEADER 也做了一些完全不同的事情(你可以在这里阅读:https://curl.haxx.se/libcurl/c/CURLOPT_HEADER.html),你需要实际设置自定义标题的是CURLOPT_HTTPHEADER,简而言之:

curl_setopt_array($ch, array(
    CURLOPT_HTTPGET => 1,
    CURLOPT_HTTPHEADER => array(
        'Authorization:xxxxxxxxxxxxxxxxxxxxxxxxxxxHTb'
    ),
    CURLOPT_URL => 'xxx.xxxxxad.com/api/rest/issues?project_id=4'
));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 2022-10-24
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多