开发者

loading remote data on server in PHP

开发者 https://www.devze.com 2022-12-16 16:18 出处:网络
I am trying to load content from a remote URL in my php code. There are two restrict开发者_JAVA技巧ions:

I am trying to load content from a remote URL in my php code. There are two restrict开发者_JAVA技巧ions:

  1. I need to use the dedicated server IP I have so the REMOTE_ADDR of the other server has to be my dedicated IP. This eliminates Curl because Curl uses a proxy to load the remote URL and the proxy changes the IP address which does not work.

  2. I need to load the data on my back-end using PHP. I do not wish to use Javascript for security reasons.

Are there any other solutions other then Curl?

Thank you


What about file_get_contents($url)? Just note that some websites require a user-agent, so you'll have to set one with ini_set() before making the call.


When using the stream api and a wrapper that utilizes the socket-wrapper you can set the bindto context parameter to accomplish (1) :

Used to specify the IP address (either IPv4 or IPv6) and/or the port number that PHP will use to access the network. The syntax is ip:port. Setting the IP or the port to 0 will let the system choose the IP and/or port.
$ctx = stream_context_create( array(
  'socket' => array(
    'bindto' => '192.168.0.107:0',
   )
));

$c= file_get_contents('http://php.net', 0, $ctx);


You could probably use fopen for this.


Have you tried file_get_contents

<?php
$homepage = file_get_contents('http://www.stackoverflow.com/');
echo $homepage;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消