本文目录
- php curl post怎么传值
- 如何使用curl发送post数据
- 请教一个PHP CURL的POST提交遇到的问题
- 如何用curl post 一段包含中文json的文本到服务器
- php使用curl的post方法字符串和数组传值的区别
- cURL进行HTTPS连接POST数据,数据怎样输入
- 使用php curl 模拟post请求,自动附加了data参数
php curl post怎么传值
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$list_url);
curl_setopt($ch, CURLOPT_REFERER,$list_urled);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0); //是否显示头文件
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //上传属性
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //cookie存放的文件夹
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //是否流
curl_setopt($ch, CURLOPT_PROXY, ’120.9.127.1:6675’); //使用代理
curl_setopt($ch, CURLOPT_VERBOSE,1); //出错提示
curl_setopt($ch, CURLOPT_USERAGENT, “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)“); //模拟浏览器
curl_setopt($ch, CURLOPT_NOBODY,true); //指定了curl抓的内容中包含header头,并且不要body内容
curl_exec($ch);
里面的$post_data就是你要post的上传的数据内容
希望对你能有所帮助。
如何使用curl发送post数据
可用我的函数。
public function post($url, $post_data) {
$this-》_ch = curl_init();
curl_setopt($this-》_ch, CURLOPT_USERAGENT, ’Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0’);
curl_setopt($this-》_ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($this-》_ch, CURLOPT_MAXREDIRS, 5);
curl_setopt($this-》_ch, CURLOPT_HEADER, 0);
curl_setopt($this-》_ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this-》_ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($this-》_ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt($this-》_ch, CURLOPT_ENCODING, ““ );
curl_setopt($this-》_ch, CURLOPT_POST, TRUE);
curl_setopt($this-》_ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($this-》_ch, CURLOPT_COOKIEFILE, getcwd () . ’/cookie.txt’ );
curl_setopt($this-》_ch, CURLOPT_COOKIEJAR, getcwd () . ’/cookie.txt’ );
curl_setopt($this-》_ch, CURLOPT_URL, $url);
$this-》_body = curl_exec($this-》_ch);
$this-》_info = curl_getinfo($this-》_ch);
$this-》_error = curl_error($this-》_ch);
curl_close($this-》_ch);
}
请教一个PHP CURL的POST提交遇到的问题
使用
所以首先你的CURLOPT_SSL_VERIFYPEER是设置为false的,所以CURLOPT_SSL_VERIFYHOST也要为false
还有, 我看请求认证并不是使用http auth, 你为什么添加curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
最后,官方文档对于CURLOPT_POSTFIELDS的解释
如果value是一个数组,Content-Type头将会被设置成multipart/form-data
而接口文档中明确说明
请求接口(建议使用post请求,注意不要使用multipart-post):
所以你可以尝试
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
最后的最后,试试下面的代码:
全选复制放进笔记《?php
header(’Content-Type:text/html;charset=utf-8’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ’https://sendcloud.sohu.com/webapi/list.create.json’);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = array(
’api_user’ =》 ’*’,
’api_key’ =》 ’*’,
’address’ =》 ’runnerlee@maillist.sendcloud.org’,
’name’ =》 ’Abao新建的测试邮件列表’,
’description’ =》 ’这是Abao新建的测试邮件列表’,
);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
if(false === $result=curl_exec($ch)) {
echo ’false:《br /》’;
}
var_dump(json_decode($result,true));
curl_close($ch);
有些情况下确实用curl是不行的,淘宝的接口也有这个问题,你可以通过在html页面用javascript自动跳提交表单来实现
如何用curl post 一段包含中文json的文本到服务器
我的博客《PHP cURL实现模拟登录与采集使用方法详解》第十一点发送与获取json数据对此类问题做了详细的讲解,下面是代码示例:
《?php
#json数据
$url = ’http://test.com/curl/testPostJsonData.php’;
$data = ’{“a“:“b“}’;
$length = strlen($data);
$header = array(
’Content-Length: ’ . $length, //不是必需的
’Content-Type: text/json’,
);
$ch = curl_init($url); //初始化curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($ch); //执行并存储结果
curl_close($ch);
echo $content;
#\n分割数据
$data = [
’name:Zjmainstay’,
’website:http://www.zjmainstay.cn’,
];
$data = implode(“\n“, $data);
#&分割数据
$data = ’name:Zjmainstay&website:http://www.zjmainstay.cn’;
更多详情,包括服务端如何接收此类数据,请查看博客:http://www.zjmainstay.cn/php-curl
php使用curl的post方法字符串和数组传值的区别
区别的话在PHP手册的curl_setopt函数中,关于CURLOPT_POSTFIELDS有如下描述:
全部数据使用HTTP协议中的“POST“操作来发送。
要发送文件,在文件名前面加上@前缀并使用完整路径。(5.5+ 建议用CURLFile)
这个参数可以通过urlencoded后的字符串类似’para1=val1¶2=val2&...’
或使用一个以字段名为键值,字段数据为值的数组。
如果value是一个数组,Content-Type头将会被设置成multipart/form-data。
因此,这两种传值方式是有所区别的。
当然,可以通过CURLOPT_HTTPHEADER指定Content-Type。可能我讲的不是很清楚很详细。你可以去后盾人平台去看看视频教学的,里面的视频教学讲的很清楚很详细。
cURL进行HTTPS连接POST数据,数据怎样输入
PHP 下
function vpost($url,$data){ // 模拟提交数据函数
$curl = curl_init(); // 启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER[’HTTP_USER_AGENT’]); // 模拟用户使用的浏览器
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo ‘Errno’.curl_error($curl);//捕抓异常
}
curl_close($curl); // 关闭CURL会话
return $tmpInfo; // 返回数据
}
$url = “
右键点项目名字,Add New Item-》C++ File, name写main.c, 输入代码:
/* 抱歉,这里不好贴链接,版权没法贴,版权去看http-post.c */
#include 《stdio.h》
#include 《curl/curl.h》
#include 《stdlib.h》
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
/* First set the URL that is about to receive our POST. This URL can
just as well be a https:// URL if that is what should receive the
data. */
curl_easy_setopt(curl, CURLOPT_URL, “这里写网址“);
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “name=daniel&project=curl“);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
system(“pause“);
}
return 0;
}
使用php curl 模拟post请求,自动附加了data参数
$post_data_string = http_build_query($post_data, ’&’);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $get_session_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmloutput = curl_exec($ch);
一般这样写 你自己对比下