loading remote data on server in PHP - php

I am trying to load content from a remote URL in my php code. There are two restrictions:
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.
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;
?>

Related

How do I find out my server's request IP using PHP

I'm trying to see what my server's request IP is. For example.
<?php
$contents = file_get_contents("https://example.com");
?>
I want to know what IP my server is using to connect to example.com. I cannot use tools like IPLookUp because that just gets the IP of the response and if I have cloudflare on my domain, then it will keep showing cloudflares proxy IP's.
If I use $_SERVER['SERVER_ADDR'] it just shows me the local IP 127.0.0.7
Use gethostbyname
gethostbyname ( string $hostname )

using https with elasticsearch-php client

I am currently trying to connect to my elastic search cluster using the php elasticsearch client
I am having trouble using an https endpoint for this. I have my cluster behind a load balancer with a VIP in front, it is using Apache authentication and is on port 443. The trouble I am running into is that the config for the client seems to be parsing the hosts and removes https:// from the host name. this results in the client always trying to connect over port 80. I have tried adding :443 to the host name but I am then getting a curl error "empty reply from server". I know that this server has access (no firewall blocking) because i can manually make the curl call using https://myelasticsearch.com.
My question is, is there a way to specify the protocol to make the request over using this client? if not, where in the source is the parsing of the host array happening?
I have found a temporary solution, in src/Elasticsearch/Connections/AbstractConnection.php there is a defined transportSchema variable that is set to http. I changed this to https and also added the :443 to my host in the config and it works!
Just as an update to this question (in case anyone stumbles into it), this bug was fixed in Elasticsearch-PHP v1.1.0. You can specify https in the host now to use SSL:
$params = array();
$params['hosts'] = array (
'https://localhost', // SSL to localhost
'https://192.168.1.3:9200' // SSL to IP + Port
);
$client = new Elasticsearch\Client($params);

VIES VAT api SOAP error ipv6

Here is the server settings/issues I am encountering:
SOAP is installed and working on the server
Using PHP SOAP extension ( new SoapClient(self::VAT_VALIDATION_WSDL_URL) ) to make the calls
The WSDL URL I am using is: http://ec.europa.eu/taxation_customs/vies/services/checkVatService?wsdl
Same code is working on most other machines, but not on the current one
SOAP response is request_success => bool(false), so the request is not made succesfully
Trying to connect to the url using telnet, for example, results in 503 Internal server error, and from other computers 502 Permission Denied.
The problem here is hard to trace, but easy to solve.
ec.europa.eu accept IPv6 requests, and responds to them using IPv6. The problem is that not all services are working correctly with IPv6. So if your server works with both IPv4 and IPv6, it will prefer using IPv6, thus the SOAP request will fail.
For example:
WKT-03:~$ ping6 ec.europa.eu #not working
connect: Network is unreachable
WKT-03:~$ ping ec.europa.eu #working
PING ec.europa.eu (147.67.136.103) 56(84) bytes of data.
Solution:
There are 2 solutions, either to disable ipv6 on the server, or to add a new rule in /etc/hosts:
147.67.136.103 ec.europa.eu # when/if IP changes, this stops working
Thus forcing it to use IPv4. Neither is very elegant, but this should work until the API is fully functional with IPv6.
SoapClient has a stream_context option.
Create stream_context with bindto targeting your IPV4 interface
Use this context in SoapClient.
Example:
$opts = array(
'socket' => array(
'bindto' => 'IP4_ADDRESS_OF_YOUR_SERVER:0'
)
);
$context = stream_context_create($opts);
$client = new SoapClient(self::VAT_VALIDATION_WSDL_URL,
array(
'stream_context' => $context
)
);

PHP fsocketopen Though Proxy

I am currently using PHP to open up a port 43 connection to get whois information directly from a registry using this code.
// connecting to the whois server.
$handle = fsockopen($server, 43);
if (!$handle)
return false; // connection failure
//asking the server
fwrite($handle, $domain_name."\r\n");
// getting response
$response = '';
while (!feof($handle))
$response .= fgets($handle, 1024);
fclose($handle);
It works great however I want to connect though a proxy server so I route my intertent connection through it. If this were able to use cURL I would use curl_setopt($curl_handle, CURLOPT_PROXY, $ip_address . ':4040'); but i can not find a way to do this using fsocketopen. How can I accomplish this either with cURL or fsocketopen()?
Sockets dont have proxy. Just gateways and routers are in-the-middle (if any). You were talking about cURL, that it has proxy - it only uses http/s proxy service. For example, if you have http proxy service on server example.com:8080 you first need to open connection to server example.com (socket) on port 8080 and then send your request, proxy will forward your request and return response. In your case, you just open tcp connection on port 43 on specific host and exchange data directly with target server. If you dont want to do this directly and reveal your ip (or something) you'll need some service too. If you have access to other machine you could use it to do the job. If you want to do it manually you could use ssh or something like that, if you want to make it automatized, you'll probably need to write service on your middle server because you probably wont find any public proxy servers with other protocols than popular http, ftp, ...
Hope this helps.
By the way I see no reason why you should use proxy on whois service.
You could use a SOCKS proxy to relay the TCP connection from your machine to the SOCKS server to the WHOIS server but you would have to implement the SOCKS communication protocol over fsockopen.
Another method would be to use ProxyChains on the server and execute it via PHP. I've answered a similar question here ( How to capture and feed telnet using php and shell scripting? ) which shows how to invoke proxychains from PHP to execute a WHOIS command on a remote server and read the response.

Best way to fetch remote RSS through authenticated proxy and parse it

I'm trying to get a remote RSS through proxy and parse it.
I'm using magpierss, but it doesn't allow reaching internet through a proxy (or I don't know how to do it).
I assume the option is to, first, fetch the rss with curl functions, that allows proxy authenticating, but ....
are there any class to do this in a easy way, or ... does magpie support using proxy, and how?
Thanks in advance.
In extlib/Snoopy.class.inc find the following lines:
var $proxy_host = ""; // proxy host to use
var $proxy_port = ""; // proxy port to use
and set there you proxy url and port.

Categories