The example of this article summarizes the method of accessing PHP across servers. Share to everyone for your reference. The specific analysis is as follows:
There have been several days of research on the issue of cross server access in the project, summarized as follows:
1. Using file_get_contents method
?
| 1 2 3 |
$host = ' url '; $randomNumber =file_get_contents ($host); echo $ $randomNumber; |
2, with Curl
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
$host = ' url '; $ch = Curl_init (); curl_setopt ($ch, Curlopt_url, $host); Return results curl_setopt ($ch, Curlopt_returntransfer, 1); curl_setopt ($ch, Curlopt_header, 0); Use POST to submit curl_setopt ($ch, Curlopt_post, 1); Post parameter $str = Array (' a=1 ', ' b=2 ', ' c=3 '); curl_setopt ($ch, Curlopt_postfields, $STR); Results $res = curl_exec ($ch); Curl_close ($ch); |
Before using the Curl Library, you may need to check the php.ini to see if the curl extension has been turned on before using the Curl Library
3, with fopen Open the URL, get way to obtain content
?
| 1 2 3 4 5 6 7 8 9 |
<?php $url = "http://www.3lian.net/"; $FP =fopen ($url, ' R '); while (!feof ($fp)) {$result. =fgets ($FP, 1024);} echo "$result"; Fclose ($FP);?> |
I hope this article will help you with your PHP program design.