您现在的位置是:PHP发送POST请求
PHP发送POST请求
//请求并返回内容 public static function curlPostData($url, $data = array(), $timeout = 5) { $httpInfo = array(); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_USERAGENT, 'MZAdmin'); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:8.8.8.8', 'CLIENT-IP:8.8.8.8')); //构造IP //curl_setopt($ch, CURLOPT_REFERER, "https://jishu8.net"); //构造来路 //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //POST数据 if( $data ) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } //判断是否为json if( is_string($data) && is_array(json_decode($data, true)) ) { curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length: '.strlen($data) ) ); } $response = curl_exec($ch); if( $response === FALSE ) { echo "cURL Error: ".curl_error($ch); return false; } $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpInfo = array_merge($httpInfo, curl_getinfo($ch)); curl_close($ch); if( $httpCode == 200 ) { return $response; } else { $err = array('errcode' => $httpCode, 'errmsg' => 'cURL Error', 'errdesc' => $httpInfo); return json_encode($err, JSON_UNESCAPED_UNICODE); } }
随便看看
站长简介
分类
最新文章
热门文章
- 微信支付退款结果通知解密
- Linux修改密码提示passwd /usr/share/cracklib/pw_dict: error reading header
- 到ICANN投诉三五互联获取域名转移密码成功
- {"errcode":45047,"errmsg":"out of response count limit hint]"}
- 微信html弹出点击右上角分享到朋友圈
- CentOS使用chkconfig提示systemctl enable xxxx.service
- PHP将B转换为KB、MB、GB
- Apache禁用TRACE Method和添加X-frame-options响应头
- mysqld_safe Number of processes running now
- 构建微信公众号被动回复image图片消息xml