PHP USES the proxy method with file_get_contents

  • 2020-03-31 21:29:29
  • OfStack

PHP USES the proxy method of file_get_contents to get the code for the remote web page.
 
<?php 
$url = "//www.jb51.net/"; 
$ctx = stream_context_create(array( 
'http' => array('timeout' => 5, 
'proxy' => 'tcp://60.175.203.243:8080', 
'request_fulluri' => True,) 
) 
); 
$result = file_get_contents($url, False, $ctx); 
echo $result; 
?> 

Another curl approach USES the proxy:
 
function postPage($url) 
{ 
$response = ""; 
$rd=rand(1,4); 
$proxy='http://221.214.27.253:808'; 
if($rd==2) $proxy='http://222.77.14.56:8088'; 
if($rd==3) $proxy='http://202.98.123.126:8080'; 
if($rd==4) $proxy='http://60.14.97.38:8080'; 
if($url != "") { 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_PROXY, $proxy); 
$response = curl_exec($ch); 
if(curl_errno($ch)) $response = ""; 
curl_close($ch); 
} 
return $response; 
} 

Use file_get_contents to solve the ajax broken domain problem

Sometimes in ajax usage, a domain-calling file is broken, and the browser will default to warning or even block this operation for security reasons. If it is IE, a warning window will pop up, asking you to continue the operation, only if you agree with IE will call the collapsed domain file. Other browsers, such as firefox and Opera, will tell you the error by default, preventing calls to outland files. This gives the user a bad experience, and if you want to fix this problem by the user changing the browser's security Settings, it is not practical, it is best to solve it on the server side.

On the server side, you can use a codomain file as a proxy file, which takes the contents of the outdomain file and passes them to ajax. Instead of calling the outfield file, ajax calls the proxy file in the same domain, and the security problem is solved.

If your server supports PHP, you can use the file_get_contents function, which already has the ability to get the contents of other files when you see its name. It can see the detailed use of PHP web site (link: http://www.php.net/manual/en/function.file-get-contents.php) use a page, the following is a simple example of it.
 
<?php 
$serverAddress = 'http://s.jb51.net'; 
//Gets the contents of the outland file
$randomNumber = file_get_contents($serverAddress); 
//output
echo $randomNumber; 
?> 

Related articles: