Home  >  Article  >  Backend Development  >  PHP method example for obtaining external data

PHP method example for obtaining external data

小云云
小云云Original
2018-03-15 09:50:111339browse

This article mainly shares with you examples of methods for obtaining external data in PHP. This article shares two methods with you, hoping to help you.

Method 1: Summary of two commonly used methods to obtain external website resource data through PHP:

$url = 'http://www.kugou.com/yy/index.php?r=play/getdata&hash=32EF5D79A20F366EC9E55A9200050D8A&album_id=1967080&_=1499914588046';
//方法1: 用file_get_contents 以get方式获取内容  -> 返回响应后的数据
$data = file_get_contents($url);
//echo $data;

Method 2: Use curl to get the content -> Return the response data

$ch = curl_init(); 
$timeout = 5; 
curl_setopt ($ch, CURLOPT_URL, $url); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
$file_contents = curl_exec($ch); 
curl_close($ch); 
 
echo $file_contents;

Print results: Print and display in the browser after ajax acquisition

Related recommendations:

Introduction to php methods to obtain data from the database

phpHow to get data

php example code for getting data in the database

The above is the detailed content of PHP method example for obtaining external data. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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