当前位置:网站首页>Curl returns blank or null without output. Solve the problem
Curl returns blank or null without output. Solve the problem
2022-07-28 16:27:00 【Sun Fendou】
curl No output returns blank or null Problem solving
If you find a problem similar to the title , It's probably because of ssl Certificate problems cause that you only need to curl Method by adding the following code :
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// prohibit cURL Verify peer certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// Check whether the domain name of the server is consistent with that on the certificate
- Give a complete gift curl Code
/** * CURL Request function : Support POST And basic header Header definition * @param [api_url: The goal is url | post_data:post Parameters | header: Header information array | referer_url: source url] * @return [code: Status code (200 Successful implementation 、400 Perform abnormal ) | data: data ] */
function curl_request($api_url, $post_data = [], $header = [], $referer_url = ''){
$ch = curl_init();// initialization CURL Handle
curl_setopt( $ch, CURLOPT_URL, $api_url);
/** Configuration return information **/
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);// The information is returned as a file stream , No direct output
curl_setopt( $ch, CURLOPT_HEADER, 0);// No return header part
/** Configuration timeout **/
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10);// Wait time before connecting ,0 Don't wait for
curl_setopt( $ch, CURLOPT_TIMEOUT, 5);// Wait time after connection ,0 Don't wait for . Such as download mp3
/** Configure page redirection **/
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);// Track crawl redirect page
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10);// Specify the most HTTP Number of redirections
curl_setopt( $ch, CURLOPT_AUTOREFERER, 1); // Automatic setting Referer
/** To configure Header、 Request header 、 Agreement information **/
curl_setopt( $ch, CURLOPT_HTTPHEADER, $header);
curl_setopt( $ch, CURLOPT_ENCODING, "");//Accept-Encoding code , Support "identity"/"deflate"/"gzip", Null supports all codes
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" );// Simulate browser header information
$referer_url && curl_setopt( $ch, CURLOPT_REFERER, $referer_url);// Forge the source address
//curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1 ); // Set up curl The use of HTTP agreement
/** To configure POST request **/
if($post_data && is_array($post_data)){
curl_setopt( $ch, CURLOPT_POST, 1 );// Support post Submit data
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($post_data));//
}
/** Prohibit certificate validation to prevent curl Output blank **/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// prohibit cURL Verify peer certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// Check whether the domain name of the server is consistent with that on the certificate
$code = 200; // Successful implementation
$data = curl_exec( $ch );
// Abnormal capture
if (curl_errno($ch)) {
$code = 400; // Perform abnormal
$data = curl_error($ch);
}
curl_close( $ch );
return ['code' => $code, 'data' => $data];
}
边栏推荐
- Voltage to current / current to voltage module
- Let's learn the game of beating hamsters
- Advantages of optical rain gauge over tipping bucket rain gauge
- 软考 系统架构设计师 简明教程 | 软件调试
- Implementation of skip table
- R语言使用GGally包的ggpairs函数可视化分组多变量的两两关系图、设置alpha参数改变图像透明度、对角线上连续变量密度图、离散变量条形图、两两关系图中包含散点图、直方图、箱图以及相关性数值
- Application of optical rain gauge to rainfall detection
- PHP图片合成技术
- Deeply understand the fusing configuration of istio traffic management
- 2021-10-21 notes
猜你喜欢

A good start

动态规划 --- 数位统计DP

Deeply understand the fusing configuration of istio traffic management

不懂就问,快速成为容器服务进阶玩家!

Laser rangefinder non-contact surface crack monitor

ANSYS二次开发 - MFC界面调用ADPL文件

Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation

视频号找到金钥匙,抖音模仿后来人

HyperMesh自动保存(增强版)插件使用说明

High precision absolute angle sensor application high speed angle monitoring
随机推荐
QT packaging
JS linked list 01
Record doc
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
PHP获取小程序码,小程序带参数跳转
R language ggplot2 visually draws line plots, and uses gghighlight package to highlight the lines that meet the combination judgment conditions in the line graphs (satisfies both condition a and b)
JS linked list 02
flashfxp 530 User cannot log in. ftp
PHP图片合成技术
RF module wireless transceiver rf63u chip application data transmission and infrastructure network
el-input限制只能输入规定的数
使用py,根据日志记录自动生成周报
Stm32cube infrared remote control: input capture
深入理解Istio流量管理的熔断配置
HyperMesh自动保存(增强版)插件使用说明
Roson的Qt之旅#102 ListModel
Voltage to current / current to voltage module
ANSA二次开发 - 抽中面的两种方法
2021 Kent interview question 3
2021 Kent interview question 1