如何使用 cURL 在 PHP 中同时使用 GET 和 POST 参数发出请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25646692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:55:49  来源:igfitidea点击:

How can I make a request with both GET and POST parameters in PHP with cURL?

phppostcurlget

提问by Benubird

Other people have already asked how to do this from perl, java, bash, etc. but I need to do it in PHP, and I don't see any question already asked relating specifically to (or with answers for) PHP.

其他人已经问过如何从 perl、java、bash 等中执行此操作,但我需要在 PHP 中执行此操作,而且我没有看到已经提出的任何与 PHP 相关(或提供答案)的问题。

My code:

我的代码:

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

This doesn't work. The destination site has print_r($_GET); print_r($_POST);, so when I examine the $resultI should be able to see the fields that are being sent. However, the $_POST array is empty - I only see the get variables. If I remove the ?...query string from the $url, then the POST array is populated correctly. But now I don't have the GET params. How do I do this?

这不起作用。目标站点有print_r($_GET); print_r($_POST);,因此当我检查 时,$result我应该能够看到正在发送的字段。但是, $_POST 数组是空的 - 我只看到 get 变量。如果我?...从 $url 中删除查询字符串,则 POST 数组将正确填充。但现在我没有 GET 参数。我该怎么做呢?

My specific case is, I need to send too much data to fit it in the query string, but I can't send it all as POST because the site I want to submit to is selecting a handler for the posted data based on a variable in the GET string. I can try and have that changed, but ideally I would like to be able to send both get and post data in the same query.

我的具体情况是,我需要发送太多数据以使其适合查询字符串,但我无法将其全部发送为 POST,因为我要提交的站点正在为基于变量的发布数据选择处理程序在 GET 字符串中。我可以尝试改变它,但理想情况下我希望能够在同一个查询中同时发送 get 和 post 数据。

回答by Marc B

# GET query goes in the URL you're hitting
$ch = curl_init('http://example.com/script.php?query=parameter');
# POST fields go here.
curl_setopt($ch, CURLOPT_POSTFIELDS, array('post' => 'parameter', 'values' => 'go here'));

PHP itself wouldn't decide to ignore the GET parameters if a POST is performed. It'll populate $_GET regardless of what kind of http verb was used to load the page - if there's query parameters in the URL, they'll go into $_GET.

如果执行 POST,PHP 本身不会决定忽略 GET 参数。无论使用哪种 http 动词来加载页面,它都会填充 $_GET - 如果 URL 中有查询参数,它们将进入 $_GET。

If you're not getting $_POST and $_GET with this, then something is causing a redirect or otherwise killing something. e.g. have you check $_SERVER['REQUEST_METHOD']to see if your code is actually running as a POST? PHP won't populate $_POST if a post wasn't actually performed. You may have sent a post to the server, but that doesn't mean your code will actually be executed under a POST regime - e.g. a mod_rewrite redirect.

如果你没有得到 $_POST 和 $_GET ,那么有什么东西导致了重定向或以其他方式杀死了某些东西。例如,您是否检查$_SERVER['REQUEST_METHOD']过您的代码是否实际作为 POST 运行?如果没有实际执行帖子,PHP 将不会填充 $_POST。您可能已经向服务器发送了一个帖子,但这并不意味着您的代码实际上会在 POST 机制下执行——例如 mod_rewrite 重定向。

Since you have FOLLOW_REDIRECT turned on, you're simply ASSUMING you're actually getting a post when your code executes.

由于您打开了 FOLLOW_REDIRECT,您只是假设您在代码执行时实际上收到了一个帖子。

回答by Santa's helper

i don't know maybe you already have but is your $url has the desired get parameters? Like:

我不知道您可能已经有了,但是您的 $url 是否具有所需的获取参数?喜欢:

$url = "http://example.com/index.php?param1=value1&param2=value2";