PHP method example for obtaining external data

小云云
Release: 2023-03-21 13:16:02
Original
1323 people have browsed it

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

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!