php curl 发送post请求带参数
发布时间:2020-04-19, 23:34:27 分类:PHP | 编辑 off 网址 | 辅助
正文 550字数 969,507阅读
public function curl_post($url , $data=array()){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 3 条评论 »
$res = curl_exec($ch); curl_close($ch); $res=mb_convert_encoding($res, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//使用该函数对结果进行转码
$url = "http://www.example.com"; //headers数组内的格式 $headers = array(); $headers[] = "app-id:xxxxx"; $headers[] = "Content-Type:application/json"; $body = array( "username" => "username", "password" => "password" ); $postBody = json_encode($body); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头 curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"做为HTTP请求。(这个加不加没啥影响) $data = curl_exec($curl); echo $data;
public static function getUserInfo(){ $url = 'http://*****'; $arr = ["111", "222", "333", "444"]; $userIds =json_encode($arr); $res = self::http($url,$userIds,'GET',['Content-Type:application/json']); } public static function http($url, $postfields = '', $method = 'GET', $headers =[]) { $ci = curl_init(); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ci, CURLOPT_TIMEOUT, 5); curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); if ($method == 'POST') { curl_setopt($ci, CURLOPT_POST, true); if ($postfields != '') curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); }else{ curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'GET' ); curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $response = curl_exec($ci); curl_close($ci); $json_r = array(); if ($response != '') $json_r = json_decode($response, true); return $json_r; }
最近将使用爬虫爬取的链接保存到 mysql 数据库中时,发现我将链接使用 json_encode 保存时候,在数据库中却显示了转义字符,我并不需要这转义的,看起来不清晰而且占用存储空间。
后来发现在默认的情况之下使用 json_encode 对数组进行 json 格式的转换时候会自动的将数据中含有斜杠的字符串进行转义,但是我们往往有的时候不需要药对它们进行转义的,本文说说如何使用 json_encode 不自动转义斜杠。
对于如下数组 $a,现有两种办法解决:
$a = array(
'http://www.baidu.com',
'http://www.baidu.com',
'http://www.baidu.com',
'http://www.baidu.com',
'http://www.baidu.com'
);
其一,正则替换: $a = str_replace("\/", "/", json_encode($a)); var_dump($a); 其二,若 php 版本是 5.4 及以上的话: var_dump(json_encode($a,JSON_UNESCAPED_SLASHES));
小程序内容审核api踩坑笔记 敏感词过滤PHP开发调用msgSecCheck,违规内容也返回无问题
$data = json_encode(array('content'=>$checkContent),JSON_UNESCAPED_UNICODE)
genwxashortlink坑
shortlink.generate 返回40066,参数检查后是对的
把 /pages/detail/index 替换成 pages/detail/index,文档的示例有问题,我们改过来。