I have the following situation, trying to clearify with a 'scheme'.
On a webapplication, we are showing resources comming from server3. But because server3 is a intranet endpoint, we are loading the resource through some extra steps. Each server has a Apache running to cap the incomming requests.
The infra setup has some 'restrictions':
Server 1 contains the webapplication
Server 2 is only reachable by server1, and can't host the webapplication. And acts as a 'entry' point to server 3, restricted access only for requests comming from server1 (intranet server)
Server 3 (resources that need to be shown on the webapplication) is only reachable by server 2. Server 3 is an intranet server.
As you see on the scheme above, loading a resources takes some steps. I'm wondering if there are any better ways within php to stream contents from server3 through server2 initiated on server1. Without having to transport the content on each step.
I've been looking at file_get_contents in combination with stream_context_create. But not sure if this is possible relaying the request through a server in the middle.
I've tried something like this (stripdown):
$opts = array(
'http' => array(
'timeout' => 10,
'proxy' => 'tcp://server2:port',
'request_fulluri' => true
)
);
$context = stream_context_create($opts);
$data = file_get_contents('server3/files/footer.png', false, $context);
echo $data;
Any expertise is welcome.
Thanks!
I am trying to retrieve details of a page/profile from Facebook into my PHP application. So I retrieve the id first and then run the following query. However I am getting an error.
I am getting the following error:
Warning: file_get_contents(https://graph.facebook.com/v2.8/40444963499?fields=id,name,picture.width(700).height(700),albums.limit(5){name,photos.limit(2){name, picture}},posts.limit(5)&access_token="my access token here"): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
However, if I run the same URL on the browser, I get back the JSON correctly.
Windows almost always has problems with SSL certificates, I wouldn't recommend you do this for your production site, but during development it's fine. By disabling the SSL check you're effectively saying you don't care if the site has a valid SSL certificate, which means that if someone was trying to impersonate graph.facebook.com you would be communicating with this site that is likely trying to steal your access token.
$context = stream_context_create(array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
)
));
file_get_contents('https://graph.facebook.com/v2.8/40444963499?fields=id,name,picture.width(700).height(700),albums.limit(5){name,photos.limit(2){name, picture}},posts.limit(5)&access_token=FB_ACCESS_TOKEN', null, $context);
Now if you're interested in actually fixing the problem on your machine, then review this answer: PHP - SSL certificate error: unable to get local issuer certificate
So the error was that there was a space in between two of the parameters passed in the url. It wasn't showing on the web browser because I assume it does not print the space but because of the space, the file_get_contents wasn't working.
I am in Ubuntu 14.04 with a Nginx server and my code is made with symfony2.
I have a simple API that returns a Json response.
I am trying to access this page with another page with the file_get_contents:
public function formAction()
$url = 'http://example.com/api/link/';
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return ($result);
}
But sometimes it works, sometimes it doesn't!
I made a loop to see the frequency:
It looks like it works 63/100 and 250/1000. (As mean value)
I got this error when it doesn't work:
Warning:
file_get_contents(http://example.com/api/link/):
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in
/srv/www/path/to/my/file.php
on line 69
This started to happen when I reboot my instance in Amazon web server, so I don't know if I should make some configuration change now. I've tried to use curl but same problem.
UPDATE
If I try to access the page in the browser it always work immediately.
I discovered what it was:
In my OVH hosting I changed the ipv4 of my dns.
But I didn't change the ipv6.
So sometimes when I tried to access the site the calls were being redirected to the right ip (the path showed in ipv4) and sometimes the old wrong one (the path indicated by the ipv6)
This works fine on my WAMP server, but doesn't work on the linux master server!?
try{
$client = new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', ['trace' => true]);
$result = $client->checkVat([
'countryCode' => 'DK',
'vatNumber' => '47458714'
]);
print_r($result);
}
catch(Exception $e){
echo $e->getMessage();
}
What am I missing here?! :(
SOAP is enabled
Error
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl' : failed to load external entity "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"/taxation_customs/vies/checkVatService.wsdl"
Call the URL from PHP
Calling the URL from PHP returns error
$wsdl = file_get_contents('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl');
echo $wsdl;
Error
Warning: file_get_contents(http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
Call the URL from command line
Calling the URL from the linux command line HTTP 200 is returned with a XML response
curl http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
For some versions of php, the SoapClient does not send http user agent information. What php versions do you have on the server vs your local WAMP?
Try to set the user agent explicitly, using a context stream as follows:
try {
$opts = array(
'http' => array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$wsdlUrl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
$soapClientOptions = array(
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE
);
$client = new SoapClient($wsdlUrl, $soapClientOptions);
$checkVatParameters = array(
'countryCode' => 'DK',
'vatNumber' => '47458714'
);
$result = $client->checkVat($checkVatParameters);
print_r($result);
}
catch(Exception $e) {
echo $e->getMessage();
}
Edit
It actually seems to be some issues with the web service you are using. The combination of HTTP over IPv6, and missing HTTP User Agent string, seems to give the web service problems.
To verify this, try the following on your linux host:
curl -A '' -6 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
this IPv6 request fails.
curl -A 'cURL User Agent' -6 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
this IPv6 request succeeds.
curl -A '' -4 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
curl -A 'cURL User Agent' -4 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
both these IPv4 request succeeds.
Interesting case :) I guess your linux host resolves ec.europa.eu to its IPv6 address, and that your version of SoapClient did not add a user agent string by default.
Security issue: This answer disables security features and should not be used in production!
Try this. I hope it helps
$options = [
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => 1,
'stream_context' => stream_context_create(
[
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
)
];
$client = new SoapClient($url, $options);
This issue can be caused by the libxml entity loader having been disabled.
Try running libxml_disable_entity_loader(false); before instantiating SoapClient.
It may be helpful for someone, although there is no precise answer to this question.
My soap url has a non-standard port(9087 for example), and firewall blocked that request and I took each time this error:
ERROR - 2017-12-19 20:44:11 --> Fatal Error - SOAP-ERROR: Parsing
WSDL: Couldn't load from 'http://soalurl.test:9087/orawsv?wsdl' :
failed to load external entity "http://soalurl.test:9087/orawsv?wsdl"
I allowed port in firewall and solved the error!
Try changing
$client = new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', ['trace' => true]);
to
$client = new SoapClient('http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', ['trace' => true, 'cache_wsdl' => WSDL_CACHE_MEMORY]);
Also (whether that works or not), check to make sure that /tmp is writeable by your web server and that it isn't full.
Try enabling openssl extension in your php.ini if it is disabled.
This way I could access the web service without need of any extra arguments, i.e.,
$client = new SoapClient(url);
None of the above works for me, so after a lot of research, I ended up pre-downloading the wsdl file, saving it locally, and passing that file as the first parameter to SoapClient.
Worth mentioning is that file_get_contents($serviceUrl) returned empty response for me, while the url opened fine in my browser. That is probably why SoapClient also could not load the wsdl document. So I ended up downloading it with the php curl library. Here is an example
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serviceUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$wsdl = curl_exec($ch);
curl_close($ch);
$wsdlFile = '/tmp/service.wsdl';
file_put_contents($wsdlFile, $wsdl);
$client = new SoapClient($wsdlFile);
You can of course implement your own caching policy for the wsdl file, so it won't be downloaded on each request.
503 means the functions are working and you're getting a response from the remote server denying you. If you ever tried to cURL google results the same thing happens, because they can detect the user-agent used by file_get_contents and cURL and as a result block those user agents. It's also possible that the server you're accessing from also has it's IP address blackballed for such practices.
Mainly three common reasons why the commands wouldn't work just like the browser in a remote situation.
1) The default USER-AGENT has been blocked.
2) Your server's IP block has been blocked.
3) Remote host has a proxy detection.
After hours of analysis reading tons of logs and internet, finally found problem.
If you use docker and php 7.4 (my case) you probably get error because default security level in OpenSSL it too high for wsdl cert. Even if you disable verify and allow self-signed in SoapClient options.
You need lower seclevel in /etc/ssl/openssl.cnf from DEFAULT#SECLEVEL=2 to
DEFAULT#SECLEVEL=1
Or just add into Dockerfile
RUN sed -i "s|DEFAULT#SECLEVEL=2|DEFAULT#SECLEVEL=1|g" /etc/ssl/openssl.cnf
Source: https://github.com/dotnet/runtime/issues/30667#issuecomment-566482876
You can verify it by run on container
curl -A 'cURL User Agent' -4 https://ewus.nfz.gov.pl/ws-broker-server-ewus/services/Auth?wsdl
Before that change I got error:
SSL routines:tls_process_ske_dhe:dh key too small
It was solved for me this way:
Every company from which you provide "Host" has a firewall.
This error occurs when your source IP is not defined in that firewall.
Contact the server administrator to add the IP.
Or the target IP must be defined in the server firewall whitelist.
I use the AdWords API, and sometimes I have the same problem.
My solution is to add
ini_set('default_socket_timeout', 900);
on the file
vendor\googleads\googleads-php-lib\src\Google\AdsApi\AdsSoapClient.php line 65
and in the
vendor\googleads-php-lib\src\Google\AdsApi\Adwords\Reporting\v201702\ReportDownloader.php line 126
ini_set('default_socket_timeout', 900);
$requestOptions['stream_context']['http']['timeout'] = "900";
Google package overwrite the default php.ini parameter.
Sometimes, the page could connect to 'https://adwords.google.com/ap
i/adwords/mcm/v201702/ManagedCustomerService?wsdl and sometimes no.
If the page connects once, The WSDL cache will contain the same page, and the program will be ok until the code refreshes the cache...
Adding ?wsdl at the end and calling the method:
$client->__setLocation('url?wsdl');
helped to me.
I might have read all questions about this for two days. None of the answers worked for me.
In my case I was lacking cURL module for PHP.
Be aware that, just because you can use cURL on terminal, it does not mean that you have PHP cURL module and it is active.
There was no error showing about it. Not even on /var/log/apache2/error.log
How to install module:
(replace version number for the apropiated one)
sudo apt install php7.2-curl
sudo service apache2 reload
I had the same problem
From local machines everything work (wamp + php5.5.38 or vagrant + php 7.4), but from prod linux server I had error
SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl' : failed to load external entity "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"
From redirect path plugin in chrome I discovered permanent redirect to https, but change url to https doesnt help.
Status Code URL IP Page Type Redirect Type Redirect URL
301 http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl 147.67.210.30 server_redirect permanent https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
200 https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl 147.67.210.30 normal none none
After few attempts of different code solutions helped my our server provider. He discovered problem in IP forwarding with ipv6.
http://ec.europa.eu/
Pinging ec.europa.eu [147.67.34.30] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Recommendation was user stream_context_create with socket and bind to 0:0. this forces ipv4
// https://www.php.net/manual/en/context.socket.php
$streamContextOptions = [
'socket' => [
'bindto' => '0:0'
],
];
$streamContext = stream_context_create($streamContextOptions);
$soapOptions = [
'stream_context' => $streamContext
];
$service = new SoapClient('https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl', $soapOptions);
I had similar error because I accidently removed attribute
[ServiceContract] from my contract, yet the service host was still opening successfully. Blunders happen
May try to see if the endpoint supports https as well
Below solution worked for me.
1- Go to php.ini in ubuntu with apache is /etc/php/7.4/apache2
( note: you should use your php version replace by 7.4 )
2- Remove ; from this line ;extension=openssl to make in uncommented.
3- Restart your web server sudo service apache2 restart
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
)
);