CURLOPT_POST与CURLOPT_POSTFIELDS:是否需要CURLOPT_POST选项? [英] CURLOPT_POST vs. CURLOPT_POSTFIELDS: Is CURLOPT_POST option required?

查看:1283
本文介绍了CURLOPT_POST与CURLOPT_POSTFIELDS:是否需要CURLOPT_POST选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一个使用PHP的 cURL 。我有一个关于curl选项使用的问题。

I'm new to cURL in PHP. I have a question regarding usage of curl options.

考虑两个脚本文件:test1.php和test2.php都存在于根www中。我使用 Ubuntu 12.04 LTS 。 PHP的libcurl版本为 7.22.0

Consider two script files: test1.php and test2.php both present in root www. I'm using Ubuntu 12.04 LTS. The libcurl version for PHP is 7.22.0.

test1.php的内容

<?php
    $ch = curl_init();
    $post_data = array(
        'firstname' => 'John',
        'lastname' => 'Doe'
    );
    curl_setopt($ch, CURLOPT_URL, 'localhost/test2.php');
    curl_setopt($ch, CURLOPT_POST, TRUE);   //is it optional?
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_exec($ch);
    curl_close($ch);
?>

test2.php的内容

<?php 
    var_dump($_POST);
?>

当我通过浏览器执行test1.php时,现在,如果我删除包含 CURLOPT_POST 的curl选项,该示例仍然有效。即使我将 CURLOPT_POST 设置为false,也会执行帖子并显示结果。那么,是否不需要 CURLOPT_POST ?看起来选项 CURLOPT_POSTFIELDS 负责通过 POST 发送数据,而不使用 CURLOPT_POST 选项。当我在test2.php中打印 $ _ SERVER 时,请求方法总是设置为 POST (带或不带选项 CURLOPT_POST )。

When I execute test1.php via browser, I can see results posted. Now if I remove curl option containing CURLOPT_POST, the example still works. Even if I set CURLOPT_POST to false, post is performed and result is displayed. So, is that CURLOPT_POST not required at all? It looks option CURLOPT_POSTFIELDS takes care of sending data via POST without use of CURLOPT_POST option. When I print $_SERVER in test2.php, request method is always set to POST (with or without option CURLOPT_POST).

任何人都可以让我知道 CURLOPT_POST option?是否需要通过 POST

Could anyone please let me know the exact use of CURLOPT_POST option? Is it neccessary required for sending data via POST?

推荐答案

发送数据正确。 CURLOPT_POSTFIELDS 意味着 CURLOPT_POST 。在使用 CURLOPT_POSTFIELDS 时,您不需要使用 CURLOPT_POST 。在这种情况下,请求方法将始终设置为POST。

You are correct. CURLOPT_POSTFIELDS implies CURLOPT_POST. You don't need to use CURLOPT_POST while using CURLOPT_POSTFIELDS. The request method will always be set to POST in this case.

请注意,只要您希望它是 POST 请求

Note that this is in your case as long as you want it to be a POST request.

如果您不想成为 POST 请求,但设置 CURLOPT_POSTFIELDS ,请参阅这个相关的Q& A:

If you don't want to be it a POST request but set CURLOPT_POSTFIELDS, please see this related Q&A:

  • How to switch from POST to GET in PHP CURL

这篇关于CURLOPT_POST与CURLOPT_POSTFIELDS:是否需要CURLOPT_POST选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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