如何使用cURL在PHP中使用GET和POST参数进行请求? [英] How can I make a request with both GET and POST parameters in PHP with cURL?

查看:613
本文介绍了如何使用cURL在PHP中使用GET和POST参数进行请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其他人已经问过如何这样做perl,java,bash等,但我需要在PHP中做,我没有看到任何问题已经问具体涉及(或与答案)PHP 。

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.

我的代码:

    $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);

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

This doesn't work. The destination site has print_r($_GET); print_r($_POST);, so when I examine the $result I 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?

我的具体情况是,我需要发送太多的数据,以适应它在查询字符串,但我不能发送所有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.

推荐答案

我可以尝试改变,但是最好能够在同一个查询中发送get和post数据。

# 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'));



如果执行POST,PHP本身不会决定忽略GET参数。它将填充$ _GET无论什么类型的http动词用于加载页面 - 如果有URL查询参数,他们将进入$ _GET。

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和$ _GET与此,那么什么是导致重定向或以其他方式杀人的东西。例如请检查 $ _ SERVER ['REQUEST_METHOD'] 以查看您的代码是否实际上作为POST运行?如果一个帖子没有实际执行,PHP不会填充$ _POST。您可能已经向服务器发送了一个帖子,但这并不意味着您的代码将实际上在POST制度下执行。一个mod_rewrite重定向。

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.

由于你打开了FOLLOW_REDIRECT,你只是在你的代码执行时,你实际上得到了一个帖子。

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

这篇关于如何使用cURL在PHP中使用GET和POST参数进行请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆