How to get only data in php curl

藏色散人
Release: 2023-03-13 22:18:02
Original
1964 people have browsed it

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.

How to get only data in php curl

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;
}
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template