Home >Backend Development >PHP Tutorial >Summary of php cross-server access methods, summary of php cross-server_PHP tutorial
The examples in this article summarize PHP cross-server access methods. Share it with everyone for your reference. The specific analysis is as follows:
Recently, I encountered the problem of cross-server access in the project. I have been studying it for several days and the summary is as follows:
1. Use 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 get the content using get method
<?php $url="http://www.bkjia.com/"; $fp=fopen($url,'r'); while(!feof($fp)){ $result.=fgets($fp,1024); } echo" $result"; fclose($fp); ?>
I hope this article will be helpful to everyone’s PHP programming design.