Summary of php cross-server access methods
This example summarizes the php cross-server access method. Share it with you for your reference. The specific analysis is as follows:
Recently, cross-server access has been encountered in the project. After several days of research, it is summarized as follows:
1. Use the file_get_contents Method
?
1 2 3 |
$ Host = 'url '; $ RandomNumber = file_get_contents ($ host ); Echo $ randomNumber; |
2. Use 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 Parameters $ Str = array ('a = 1', 'B = 2', 'c = 3 '); Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ str ); // Result $ Res = curl_exec ($ ch ); Curl_close ($ ch ); |
Before using the curl library, you may need to check php. ini to see if the curl extension has been enabled.
3. Use fopen to open the url and get the content.
?
1 2 3 4 5 6 7 8 9 |
<? Php $ Url = "http://www.bkjia.net /"; $ Fp = fopen ($ url, 'R '); While (! Feof ($ fp )){ $ Result. = fgets ($ fp, 1024 ); } Echo "$ result "; Fclose ($ fp ); ?> |
I hope this article will help you with php programming.