This article mainly introduces PHP cross-server access methods, and examples summarize common PHP cross-server access techniques. I hope to be helpful.
The specific analysis is as follows:
Recently, I have encountered the problem of cross-server access in the project. I have studied it for many days and summarized it as follows:
1. Use the file_get_contents method
$host = 'url'; $randomNumber=file_get_contents($host); echo $$randomNumber;
2. Use Curl
$host = 'url'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $host); // 返回结果 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // 使用POST提交 curl_setopt($ch, CURLOPT_POST, 1); // POST参数 $str = array('a=1','b=2','c=3'); curl_setopt($ch, CURLOPT_POSTFIELDS, $str); // 结果 $res = curl_exec($ch); curl_close($ch);
Use the curl library. Before using the curl library, you may need to check php.ini to see if the curl extension has been turned on
3. Use fopen to open the url and obtain the content in get mode
<?php $url="http://www.jb51.net/"; $fp=fopen($url,'r'); while(!feof($fp)){ $result.=fgets($fp,1024); } echo" $result"; fclose($fp); ?>
Related recommendations:
Detailed explanation of cross-domain solutions in laravel development
AJAX principles and CORS cross-domain methods
The above is the detailed content of PHP implements cross-server access. For more information, please follow other related articles on the PHP Chinese website!