PHP: curl_setopt - Manual http://php.net/manual/en/function.curl-setopt.php

CURLOPT_POST TRUE to do a regular HTTP POST. This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.
CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Typeheader will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

php - CURLOPT_POST vs. CURLOPT_POSTFIELDS: Is CURLOPT_POST option required? - Stack Overflow https://stackoverflow.com/questions/26728740/curlopt-post-vs-curlopt-postfields-is-curlopt-post-option-required

1

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

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.

Contents of 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);
?>

Contents of test2.php

<?php
var_dump($_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).

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

21down voteaccepted

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.

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

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

 
 

curl post CURLOPT_POSTFIELDS的更多相关文章

  1. PHP中curl的CURLOPT_POSTFIELDS参数使用细节

    CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的.但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果. 在通常情况下,我们使用 CURL 来提交 PO ...

  2. linux下c/c++方式访问curl的帮助手册

    自:http://blog.chinaunix.net/u1/47395/showart_1768832.html 有个业务需求需要通过curl 代理的方式来访问外网 百度了一把,测试可以正常使用.记 ...

  3. 《CURL技术知识教程》系列分享专栏

    <CURL技术知识教程>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201737.html 文章 PHP采集相关教程之一 CUR ...

  4. php之curl设置超时实例

    本文实例讲述了php中curl超时设置方法.分享给大家供大家参考.具体实现方法如下: 访问HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法. 在访问 ...

  5. [转]linux 下 使用 c / c++ 调用curl库 做通信开发

    example:   1. http://curl.haxx.se/libcurl/c/example.html  2. http://www.libcurl.org/book:  1. http:/ ...

  6. php curl的隐藏BUG

    <?php $a = array( 'a' => 2, 'b' => 3, ); $curl = curl_init(); $b = $a; curl_setopt_array($c ...

  7. PHP收藏

    PHP打印测试,PHP调试技巧    PHP header函数使用大全    PHP面向对象(OOP)编程入门教程 小片段: PHP保留2位小数 格式化小数.浮点数 header('Content-T ...

  8. PHP的错误机制总结

    PHP的错误机制也是非常复杂的,做了几年php,也没有仔细总结过,现在就补上这一课. 特别说明:文章的PHP版本使用5.5.32 PHP的错误级别 首先需要了解php有哪些错误.截至到php5.5,一 ...

  9. Http Header里的Content-Type

    之前一直分不清楚post请求里Content-Type方式,如application/x-www-form-urlencoded.multipart/form-data.本文会介绍Content-Ty ...

随机推荐

  1. 服务发现与负载均衡 dubbo zk原理

    服务发现与负载均衡 拓展阅读 : dubbo 原理概念图 2016-03-03 杜亦舒 性能与架构 性能与架构 性能与架构 微信号 yogoup 功能介绍 网站性能提升与架构设计 内容整理自文章“实施 ...

  2. Atitit. WordPress 4.2.2新特性对比 attilax总结

    Atitit. WordPress 4.2.2新特性对比 attilax总结 1. WordPress 2.9带来的新特性 1 2. WordPress3.0最为突出的五个新特征 2 3. WordP ...

  3. 配置taglib的uri的2种方法

    推荐方法1 1.把uri写在tld文件中,tld放在WEB-INF文件夹下,例如: <short-name>就对应了你在jsp中引用的时候<%@ taglib prefix=&quo ...

  4. requireJS的匿名模块和命名模块的区别和最佳实践

    requirejs是一个简单的javascript框架,支持模块化编码和模块的异步载入. 在requireJS中模块能够分为:匿名模块和命名模块这2种. requireJS定义一个匿名模块 defin ...

  5. 转 java调用php的webService

    1.首先先下载php的webservice包:NuSOAP,自己到官网去下载,链接就不给出来了,自己去google吧    基于NoSOAP我们写了一个php的webservice的服务端,例子如下: ...

  6. 下一个时代,对话即平台 —— 开始使用Bot Framework和Cognitive Service来打造你的智能对话服务

    在16年3月30号微软的全球开发者大会Build上发布了Bot Framework,微软认为下一个big thing是Conversation as a Platform,简称CaaP,中文应该叫做& ...

  7. 【跟我一步一步学Struts2】——Struts2工作流程

    上一篇博客通过一个简单的小样例对struts2的流程有一个简单的了解,这篇博客继续. 当用户要登陆某一个站点.输入username,password,点击登陆就会触发以下一系列过程 : 1.请求过来之 ...

  8. Character流与Byte流的区别

    Character流与Byte流的区别 (2012-09-25 16:15:49) 标签: 杂谈 分类: 大赛指导 Character流与Byte流的区别是 A) 每次读入的字节数不同 B) 前者带有 ...

  9. 微信小程序3 - 对象的合并

    ES6中 Object.assign方法用于对象的合并,将源对象( source )的所有可枚举属性,复制到目标对象( target ). 限制:   只是浅拷贝, 即 内部对象 不会拷贝,只是 引用 ...

  10. Windows下 Zmq v4.0.4的使用 (C++)

    最近在研究Zero的使用. 我在网上找了一下,发觉大部分博客都写得乱七八糟,又不写明什么版本,什么系统下的,我都不知道怎么样才能弄出个编译过的程序来,只好自己摸索了. 我是windows下 使用vs2 ...