How to use php cURL on local vagrant? - php

I have the following piece of code which works on my remote server
$myCurl = curl_init();
curl_setopt_array($myCurl, array(
CURLOPT_URL => "http://{$_SERVER['HTTP_HOST']}/social/api/auth/login_or_register",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($credentials)
));
$exec = curl_exec($myCurl);
I decided to port the project to local for further development so I have set up a vagrant virtual machine with everything needs, everything seems to work normally except the piece of code above which gives me the following error:
Curl failed with error #7: Failed to connect to project.dev port 8000: Connection refused
Any hints on how I can work around this problem so that I can develop normally on local ?

add in /etc/hosts
127.0.0.1 project.dev

Related

phpbrew php7 throws gnutls_handshake() failed: Illegal parameter

I've compiled php7.1.30 on ubuntu 14.04 successfully.
When I test that peace of code
/ Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://buy.itunes.apple.com',
CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => [
'item1' => 'value',
'item2' => 'value2'
]
]);
curl_setopt($curl, CURLOPT_VERBOSE, true);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
if (!$resp) {
echo('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
echo "\n";
}
curl_close($curl);
I got error Error: "gnutls_handshake() failed: Illegal parameter" - Code: 35
The verbose output is that:
Rebuilt URL to: https://buy.itunes.apple.com/
* Hostname was NOT found in DNS cache
* Trying 17.173.66.180...
* Connected to buy.itunes.apple.com (17.173.66.180) port 443 (#0)
* found 148 certificates in /etc/ssl/certs/ca-certificates.crt
* gnutls_handshake() failed: Illegal parameter
* Closing connection 0
From time to time this works successfully it seems that apple TLS support is not consistent.
However if I build the same code on Ubuntu 18.04 it works 100% and if I run the same code with php 5.5.9 (dist version) again 100% working.
So far I plan to upgrade to ubuntu 18.04 in order to get the working because I fail to overcome above issue. I've tried different /etc/ssl/cert ca files but unsuccessfully.
Help is appreciated.
Unfortunately the only way to get that working in the change openssl-dev lib on ubuntu with the newer one and recompile again.
However I decide to get rid of ubuntu 14.04 in your infrastructure - speed the planned discount of Ubuntu 14 and replace with amazon linux 2.

PHP7 cURL and SOAP request SSL failing

I'm using PHP 7.0.25 in a vagrant VM with ubuntu 16.04
I need to make a request to this SOAP API:
https://ws.ocasa.com/testecommerce/service.asmx?wsdl
Both php curl and SoapClient are failing
With SoapClient I'm doing this (also tried with no options at all, and just the cache_wsdl part):
$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
)
))
);
$wsdl = 'https://ws.ocasa.com/testecommerce/service.asmx?wsdl';
$client = new \SoapClient( $wsdl, $options);
Giving me this error:
[SoapFault]
SOAP-ERROR: Parsing WSDL: Couldn't load from
'https://ws.ocasa.com/testecommerce/service.asmx?wsdl' : failed to
load external entity "https:
//ws.ocasa.com/testecommerce/service.asmx?wsdl"
With php curl I endend getting this error:
Unknown SSL protocol error in connection to ws.ocasa.com:443
I cant get the page from the linux curl command line without a problem (from inside the VM, of course)
curl -I https://ws.ocasa.com/testecommerce/service.asmx?wsdl -v
The SSL connection is using TLS1.0 / RSA_3DES_EDE_CBC_SHA1
I tested adding
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
and also tried adding the CA certs: (also tried adding them in the php.ini)
curl_setopt($ch, CURLOPT_CAINFO, '/vagrant/cacert.pem');
Something very strange is that I can curl some other https sites without problem like secure.php.net usgin php curl.
Also, there is no "http" version available to get rid of the whole SSL problem.
Any ideas? I'm missing some dependency maybe? openssl is installed.
Thanks for your time.
Well, I kinda found a solution...
With curl (the CURLOPT_SSLVERSION makes the difference)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wsdl);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); // $xml is the envelope (string)
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0);
I couldn't make SoapClient work, even with the SOAP_SSL_METHOD_TLS option.
I'll mark it as resolved, but if some finds how to make it work with SoapClient, that would be the real answer.
This helped me out with making the request (not the ssl part)

unable to Curl or SoapClient Any remote_server:8080 port on Directadmin Centos

I am Trying To Connect a Soap Api by php
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$opts = array(
'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
);
$params = array ('encoding' => 'UTF-8', 'verifypeer' => false, 'verifyhost' => false, 'soap_version' => SOAP_1_1, 'trace' => 1, 'exceptions' => 1, "connection_timeout" => 5, 'stream_context' => stream_context_create($opts) );
$url = "http://x.x.x.x:8080/ws-relay/MessageRelayService?wsdl";
$client =new SoapClient($url,$params);
$result = $client->sendMessageOneToMany(array(
"username" => 'xxxxx',
"password" => 'xxxxx',
"originator" => "50004132311446",
"destination" => $numbers,
"content" => $massage,
));
but it allways got an
SOAP-ERROR: Parsing WSDL: Couldn't load from 'x.x.x.x:8080/ws-relay/MessageRelayService?wsdl";' : failed to load external entity "x.x.x.x:8080/ws-relay/MessageRelayService?wsdl";")
error !
I also Tried to Curl This WSDL but I got
[root#myhost ~]# curl http://x.x.x.x:8080/ws-relay/MessageRelayService?wsdl
curl: (7) Failed to connect to x.x.x.x port 8080: Connection timed out
Error !
so i also tried to curl portquiz.net:8080/ for test and i got timeout again !
is there some rule in firewall that block me to send request to 8080 ports !
how can i unblock it in centos directadmin vps ?
often this happens because that your ip address has been blocked , or there are an issues with the server it self .
for more info about that , libcurl error codes
CURLE_COULDNT_CONNECT (7)
Failed to connect() to host or proxy.
for your command :
curl http://xx.xx.xx.xx:xxxxws-relay/MessageRelayService?wsdl
when executing it in my own pc i got the response normally .
so you will need to use proxy in your SoapClient class
For making an HTTP connection through a proxy server, the options
proxy_host, proxy_port, proxy_login and proxy_password are also
available.
so you will need to add some params to your $params array ass follows :
$params['proxy_host'] = "proxy_ip";
$params['proxy_port'] = "proxy_port";
it's also possible (personally i recommend this) to use procedures libcurl functions or packages built on it.
if you want to use the cli way , so your command may be some thing like this :
curl http://xx.xx.xx.xx:xxxx/ws-relay/MessageRelayService?wsdl -x "ip_address:ip_port"
for socks5 ips
curl http://xx.xx.xx.xx:xxxx/ws-relay/MessageRelayService?wsdl -x "ip_address:ip_port" --socks5

Access SoapClient and file_get_contents from local LAMP

I'm working under ubuntu 12.04.
I have a classic localhost set up . apache + mysql + php
file_get_contents fail on external url but works well with local file or 'localhost'
file_get_contents('http://google.com');
PHP Warning: file_get_contents(http://google.com): failed to open stream: HTTP request failed!
soapClient fail on external url but works well with local file or 'localhost'
$wsdl = "http://test.webservices.delijn.be/wsrise/services/travel/WEB-INF/wsdl/RiseWebservices.wsdl";
$client = new SoapClient($wsdl,
array(
'trace' => true,
'exceptions' => true,
'soap_version' => SOAP_1_1,
'connection_timeout' => 600,
'compression' => SOAP_COMPRESSION_ACCEPT ,
'encoding'=> 'UTF-8', //ISO-8859-1',
//'cache_wsdl' => WSDL_CACHE_BOTH,
//'host' => "localhost",
//'proxy_host' => "localhost",
//'proxy_port' => 8080,
));
SOAP-ERROR: Parsing WSDL: Couldn't load from
'http://test.webservices.delijn.be/wsrise/services/travel/WEB-INF/wsdl/RiseWebservices.wsdl' : failed to load external entity "http://test.webservices.delijn.be/wsrise/services/travel/WEB-INF/wsdl/RiseWebservices.wsdl"
allow_url_fopen and allow_include_url are on in phpinfo() (set in /etc/php5/apache2/php.ini)
firewall is disabled
apparmor is disabled
suhosin patch is installed and i set suhosin.simulation to true in the php.ini
the code is working well on my production and staging servers
I 'm out of ideas and don't find intresting log or command that can inspect the problem :'(
Thanks for your help !
Ok ! I found a way to make it work.
install de suhosin-php package in order to be able to use the Module suhosin.
set the suhosin.simulation directive to on in /etc/php5/con.d/suhosin.conf
sudo service apache2 restart
Soap calls are still extremely slow, but are working now
Thanks for your help !

Unable to Connect to ssl

I have configured the openssl with wamp (Apache server). But while I using gdata api I'm getting following error.
( ! ) Fatal error: Uncaught exception 'Zend_Http_Client_Adapter_Exception' with message ' in C:\Zend_1_11_11\library\Zend\Http\Client\Adapter\Socket.php on line 234
( ! ) Zend_Http_Client_Adapter_Exception: Unable to Connect to ssl://accounts.google.com:443. Error #10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Zend_1_11_11\library\Zend\Http\Client\Adapter\Socket.php on line 234
Somebody help me on this...
Solution only for Windows users:
Check the SSL module is enabled in php.ini:
extension=php_openssl.dll
Answer from Mikhail does not work for me as I run it in Alpine Linux and .dll is only windows extension. Do not use it outside of Windows, it only adds warnings.
Solved my problem:
I had a self-signed certificate that was unable to establish connection.
To check that it is problem you can make a request:
wget way:
// not working:
wget https://accounts.google.com:443
// working:
wget https://accounts.google.com:443 --no-check-certificate
or curl way:
// not working:
curl https://accounts.google.com:443
// working:
curl https://accounts.google.com:443 -k
To temporary solve it in my dev docker container, I have added use of curl adapter and no check for certificate to the code:
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'curloptions' => [CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false]
);
$client = new Zend_Http_Client('https://example.com', $config);
You are behind proxy, so you can not connect directly.Try to use Zend/Http/Client/Adapter/Proxy.php instead of Zend\Http\Client\Adapter\Socket.php
If you on Ubuntu try add in your php.ini
openssl.capath=/etc/ssl/certs
For more info read this issue

Categories