file_get_contents fails w/ empty exception - php

I have a strange behaviour of file_get_contents. We are behind a proxy, so I needed to set the context of file_get_contents. The strange thing is, on my PC it works fine, but on other PC or at other location it won't work, running into the max_execution_time timeout.
if ($requestType == 'GET') {
$context = [
'http' => [
'method' => 'GET',
'header' => "Authorization: " . dbQueryHelper::getApiKey () . "\r\n" . "Content-Type: application/x-www-form-urlencoded\r\n",
'proxy' => getenv ( 'PROXY' ),
'request_fulluri' => true
],
'ssl' => array(
'SNI_enabled' => false, // Disable SNI for https over http proxies
'proxy' => getenv('PROXY'),
'request_fulluri' => true,
"verify_peer"=>false,
"verify_peer_name"=>false,
)
];
}
$context = stream_context_create ( $context );
$dataraw = [
'email' => getenv('DEVMAIL'),
'userEmail' => $user->getNickname(),
'production' => getenv('PROD')
];
$data = http_build_query ( $dataraw );
$context = dbQueryHelper::getContext ( 'GET' ); // this gets the context from above
$result = file_get_contents ( getenv('RESTAPIURL') . '/getPernrByEmail?' . $data, false, $context );
$result = json_decode ( $result, true );
I also get no error message on the other PCs, only
Warning: file_get_contents(...): in C:\trainingplan\lib\common.php on line 79
I think the strange thing is that the warning contains no error... We don't think it could be the proxy because I use the same proxy everywhere and can also use different proxies - same behaviour, on my PC it works, on others not. I also tried with disabling Firewall and UAC and other security services on other PCs, doesn't work as well... We have no clue what could be the root cause.
Do you have any idea for me?

We finally found the solution. It was not network related. It is a bug with PHP 5.5 contained in the Google App Engine Launcher Installer. When specified
runtime: php55
in the app.yaml, the above mentioned symptoms occur. When we change the runtime to
runtime: php
it works fine with proxy and everything. This could be kind of problematic, as Google already deprecated usage of "runtime: php" and you can only upload applications with "runtime: php55" to the App Engine servers. But I think a reasonable workaround would be to develop locally with "runtime: php" and change it to "runtime: php55" only for the upload - but I have no idea if that could have other impacts...
Another cause we experienced yesterday was IPv6. In App Engine Launcher v1.9.23 it helps to disabled IPv6 completely in the entwork settings of your PC. After a restart the php-cgi.exe of GAE Launcher connects correctly through the IPv4 proxy.

Related

file_get_contents not working on CentOS 7

I was using phpoffice template processing functionality with Laravel and it worked on Ubuntu, Windows and Mac. The functionality stopped working when I migrated to centos server. I figured out that the phpoffice was calling file_get_contents to open the template file and it is failing.
So started testing the file_get_contents function.
echo file_get_contents('http://my-server-ip-address/api/public/storage/templates/template.docx');
Error,
ErrorException (E_WARNING)
file_get_contents(http://my-server-ip-address/api/public/storage/templates/template.docx): failed to open stream: Connection timed out
php.ini configuration,
allow_url_fopen = On // tried allow_url_fopen = 1 also
Tried,
user_agent = "PHP"
I cannot change the function call to CURL approach as this is internally handled in the phpoffice composer package. Is there a solution for this?
I can access the file directly on the browser. No issues with that.
Edit:
echo file_get_contents('http://my-ip/api/public/storage/templates/template.docx');
echo file_get_contents('http://my-domain/api/public/storage/templates/template.docx');
echo file_get_contents('https://www.google.com/');
echo file_get_contents('http://localhost/api/public/storage/templates/template.docx');
Here 1 & 2 not working from the same server which the IP/domain points to but it works from any other systems including local.
Summary
Same issue with wget and CURL.
The server can't find itself using IP or domain but other systems can communicate with server.
The server identifies itself only as localhost.
you can give it a try. hope it helps you out.
$url= 'https://example.com';
$arrContextOptions=array(
'http' => array(
'method'=>"GET",
'header'=>"Content-Type: text/html; charset=utf-8"
) ,
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
"allow_self_signed" => true, // or "allow_self_signed" => false,
),
);
$response = file_get_contents($url, false, stream_context_create($arrContextOptions));
you can read out all the manual about the stramContext here on the php manual https://www.php.net/manual/en/function.file-get-contents.php

unable to run soap based API on the server but work well on local pc

I'm working on payment gateway API and API is based on the SOAP request. I have tested it on my local PC (Ubuntu + php 7.2) and it's working fine locally.
my code look like:
$options = array(
'cache_wsdl' => 0,
'trace' => 1,
'stream_context' => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
)
)
);
$soapclient = new SoapClient("domain-name", $options);
but when I moved the code on the server (Cent OS + PHP 5.6) it stop working with following error:
SOAP-ERROR: Parsing WSDL: Couldn't load from domain-name : failed to load external entity domain-name
i know there are lots of similar question have answered on the site (like this, this, this and may more) but I have tried all of them but no luck.
please help me to fix this issue.
thanks.
I had the same problem. my soap API was working on local PC but not working on the server.
then I check the port 8080 status of my server with the following command:
# netstat --listen
or
# netstat -l
then I found that the port was closed. and after opening it SOAP API start working
I hope this will helps you

php SoapClient fails to connect, but command line curl call works

I have a LAMP server (#1) that is communicating via soap with another server (#2) via WSDL. If I issue a curl call on the command line of server 1 to the URL of server 2, it works fine and get the appropriate WSDL response, but a php soapclient to the same URL is getting a "failed to load external entity" error. This was working before when we had a self signed certificate on server 2, but quit working about the same time we upgraded to a CA certificate.
Funny thing is this server is load balanced with another server at a different location (different OS, but same php code/database) and the second server isn't having any issues at all.
Here is the code I am using for the soap client:
function getSoapClient(){
ini_set("soap.wsdl_cache_enabled", 0);
// standard soap client for application service
$post_url = lum_getString("[CAMPAIGN_POST_URL]").
"?enterprise=".lum_getString("[CAMPAIGN_ENTERPRISE]").
"&company=".lum_getString("[CAMPAIGN_COMPANY]");
$options = array(
'trace' => true,
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => 1,
'verifypeer' => false,
'verifyhost' => false,
'allow_self_signed' => true,
'login' => lum_getString("[CAMPAIGN_POST_ID]"),
'password' => lum_getString("[CAMPAIGN_POST_LC]"),
);
$context = stream_context_create(
array(
'user_agent' => 'PHPSoapClient',
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true,
),
'https' => array(
'curl_verify_ssl_peer' => false,
'curl_verify_ssl_host' => false,
)
)
);
$options['stream_context'] = $context;
$client = new SoapClient($post_url."&wsdl",$options);
return $client;
}
The curl and soapclient are using the same ports so it shouldn't be a firewall issue.
Any help in identifying the issue or helping me figure what is wrong is greatly appreciated.
Turns out it appears to be a firewall issue. I opened up all access from server 1 to server 2 and things started working. Not too sure what the issue was. I reduced the options to just the login and password, it's still working. Firewalls are often the answer.
Any ideas regarding the firewall, the correct ports, and why it wasn't working is much appreciated.
Why was cURL working and SOAP not?

Zend Unable to connect to HTTPS server through proxy

I'm using Zend Framework 1.* and when executes the "stream_socket_enable_crypto" always returns false. Zend throws the exception this exception:
"Unable to connect to HTTPS server through proxy could not negotiate secure connection."
I've tried all the following crypto types in the previous method "stream_socket_enable_crypto" with the same result...
STREAM_CRYPTO_METHOD_SSLv2_CLIENT
STREAM_CRYPTO_METHOD_SSLv3_CLIENT
STREAM_CRYPTO_METHOD_SSLv23_CLIENT
STREAM_CRYPTO_METHOD_TLS_CLIENT
STREAM_CRYPTO_METHOD_SSLv2_SERVER
STREAM_CRYPTO_METHOD_SSLv3_SERVER
STREAM_CRYPTO_METHOD_SSLv23_SERVER
STREAM_CRYPTO_METHOD_TLS_SERVER
Looking deep into the "connectHandshake" Zend method I've seen I can connect to the proxy, but not to the server through the proxy. I'm stuck for long with this issue, any clue?
PHP version 5.6.*
Finally realized was a problem of PHP version. In PHP 5.6.* "verify_peer_name" default changed to TRUE. See change log here.
Setting this property to false solved the connection issue.
$adapter->setStreamContext(array(
'socket' => array(
// Bind local socket side to a specific interface
'bindto' => self::PROXY_HOST . ':' . self::PROXY_PORT
),
'ssl' => array(
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
// Verify server side certificate,
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => false,
)
)
);
Hope it helps someone else

Error in cURL request: name lookup timed out

I wrote some code that fill a login form and submit it via post method. Like:
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
);
$this->siteObj = new Zend_Http_Client('http://example.com', $config);
$this->siteObj->setCookieJar();
$this->siteObj->setUri('http://example.com/login');
$this->siteObj->setParameterPost( 'data[User][name]', 'user' );
$this->siteObj->setParameterPost( 'data[User][password]', 'password' );
$response = $this->siteObj->request('POST');
its works fine, but some times this error occur:
Error in cURL request: name lookup timed out
Error: An Internal Error Has Occurred.
whats the problem? what can I do to solve it?
I encountered the same problem:
From the shell, curl worked.
From the shell, the PHP script worked.
PHP could not ping the website.
The DNS config was right.
After restarting Apache, it worked. So strange.
It can be a timeout problem.
Try adjusting the connection timeout:
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'timeout' => 100
);
you can also set single curl options:
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => array(
CURLOPT_USERAGENT => 'Zend_Curl_Adapter',
CURLOPT_HEADER => 0,
CURLOPT_VERBOSE => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
),
);
If you find out that it is a timeout issue, I would not suggest to increase too much the timeout parameter, but rather to make a for loop with a number of retries.
It means that your DNS server failed to return a response in time. Check your DNS configuration (e.g. /etc/resolv.conf on Linux), and make sure they are alive and functional. Also try to ping the host in the URL from the same sever to get an idea whether the problem only in PHP or the any application running on the server (more likely).

Categories