php curl method to obtain data: 1. Obtain data through the "function http_curl($url, $type = 'get', $data = ''){...}" method; 2. Use POST and GET can get the data.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How does php curl only get data?
php uses CURL to obtain data
The first one, merge POST and GET
function http_curl($url, $type = 'get', $data = ''){ $cl = curl_init(); //初始化 curl_setopt($cl, CURLOPT_URL, $url); //设置 cURL 传输选项 curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1); // 将curl_exec()获取的信息以字符串返回,而不是直接输出。 curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, false); if($type == 'post'){ curl_setopt($cl, CURLOPT_POST, 1); //发送 POST 请求,类型为:application/x-www-form-urlencoded curl_setopt($cl, CURLOPT_POSTFIELDS, $data); } $output = curl_exec($cl); //执行 cURL 会话 curl_close($cl); return $output; }
The second one is to separate POST and GET
POST
$url = "http://localhost/web_services.php"; $post_data = array ("username" => "bob","key" => "12345"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // post数据 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // post的变量 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch); curl_close($ch); //打印获得的数据 print_r($output);
GET
//初始化 $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "http://www.jb51.net"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //执行并获取HTML文档内容 $output = curl_exec($ch); //释放curl句柄 curl_close($ch); //打印获得的数据 print_r($output);
The data obtained in the above method is in json format
Use json_decode($output,true) to parse it into an array; use json_decode($output) to Parsed into an object
Parameter description:
$url: The url address to be requested. If it is a get request, you can add the parameters directly to the end of the url
$type: Request method
$data: Parameters carried when requesting in post mode
curl_init() Initialize a cURL session
curl_setopt() Set a cURL transmission option
curl_exec() Execute a cURL session
curl_close() Close a cURL session
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get only data in php curl. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools
