curl_errno 56 on localhost - php

I'm having trouble with php curl on my localhost for days. It seems that I'm not reaching the CURLOPT_URL parameter. curl_error and curl_errno says, "Recv failure: Connection was reset". The url is supposed to return a processed value of the passed post data. I'm using windows 7 machine and running XAMPP 3.2.2
Appreciate your help guys. Thanks
Below is my code:
private function getCurl ($credentials = array(), $app_code = "")
{
$ch = curl_init();
$data = array('Code' => $code,'type' => 'credentials');
curl_setopt($ch, CURLOPT_URL,'http://localhost/web/service.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' .
base64_encode($credentials['user'] . ":" . $credentials['pass'])));
$curl_response = curl_exec($ch);
//return array(curl_error($ch),curl_errno($ch));
curl_close($ch);
return $curl_response;
}

Apache error logs: [Thu Apr 14 07:46:54.089533 2016] [ssl:warn] [pid 3372:tid 252] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
There could be a number of reasons why this is happening. I think you should use the XAMP Control Panel to view the specific errors.
Quite possible that another application such as Skype is using port 80. In this case you may want to check using netstat -ano and find who is using port 80. Close the Program which uses port 80 and try to start apache again.
It may be due to your rights. You will need to be administrator.
Disable Anti-virus (Try first to disable skype and running again, if it didn't work do this step)
Right click on xampp control panel and run as administrator
server name used in Apache (httpd.conf) must be the same as the server name in apache (httpd-ssl.conf) e.g. in Apache (httpd.conf)ServerName localhost:8080 then in apache (httpd-ssl.conf) should be like this ServerName www.example.com:8080
You can also try to change the Configuration of Apache.
a) Select Apache (httpd.conf)
b) searched for this line: Listen 80
c) changed for this: Listen 8081
d) saved file
Further...
a) Select Apache (httpd-ssl.conf)
b) searched for this line: Listen 443
c) changed for this: Listen 444
d) saved file

Related

CURL can not run with Domain name on local Server

i'm trying to use curl to run a local php file inside my centOs7 shared server, but i can not use curl method for my local files.
here are my example files :
--bg/
------back.php
------curl.php
here is curl.php codes:
<?php
function run_curl($data){
//url-ify the data for the POST
$fields_string = '';
//foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = http_build_query($data);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
//curl_setopt($ch, CURLOPT_HTTPHEADER, ['Accept: ' . $content_type]);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
curl_setopt($ch,CURLOPT_URL,'https://taskdan.com/bg/back.php');
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,50); # timeout after 10 seconds, you can increase it
//curl_setopt($ch,CURLOPT_HEADER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); # Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); # Some server may refuse your request if you dont pass user agent
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//print_r($result);
//print_r((curl_getinfo($ch)));
//error_log($result);
//close connection
curl_close($ch);
return ($result);
}
echo " Output : </br>";
echo run_curl( [
"ab" => "test"
]);
?>
and the below my back.php codes:
<?php
header("Content-Type:application/json");
echo json_encode([
"a" => "test"
])
?>
When I run this code, it looks like it converts my domain name to the server's public IP address. Curl is working fine for outside of server scripts.
when i run curl with using SSH, i get this:
[root#??? ~]# curl https://taskdan.com -k
<html>Apache is functioning normally</html>
[root#??? ~]# curl http://171.22.27.118 -k
<html>Apache is functioning normally</html>
i want to run some scripts in background, Are there any way to run a local php file with POST parameters by CURL?
Are you sure that your Apache virtual host is configured for listening on all interfaces?
When you do a DNS lookup to a domain that is configured as 127.0.0.1 in your /etc/hosts the request is then pointed to the loopback interface.
Open your virtualhost configuration and search for:
<VirtualHost ...>
If it is configured as
<VirtualHost 171.22.27.118:80>
Change it to
<VirtualHost *:80>
..and try again
If you cannot edit the VirtualHost definition for any reason you can instruct cURL to make the request on a specific address/port and set the correct Host header:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Host: taskdan.com'
)
);
curl_setopt($ch,CURLOPT_URL,'https://171.22.27.118/bg/back.php');
This could help mitigate the issue but is not guaranteed to work as the problem is the webserver configuration not the source code per se.
Your /etc/hosts file should look like this:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
Change it to look like this:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 taskdan.com
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 taskdan.com
Or, just change your code.
curl_setopt($ch,CURLOPT_URL,'http://127.0.0.1/bg/back.php');
And configure Apache accordingly.

PHP CURL with proxy causes CLOSE_WAIT on socket

I'm using PHP curl library to establish connection and retrieve content from WEB - the usual.
I have multiple SOCKS5 proxy servers running on localhost on ports from 10300 to 10350 and PHP selects a port randomly.
My code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, "localhost:".mt_rand(10300, 10350));
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_resp = curl_exec($ch);
curl_close($ch);
/// ... whatever logic
But if I put this code into loop for long time, I get error telling that no more files can be opened. I read online that is because filehandler limit being reached, so I debugged with lsof -p and I noticed that the process has hundreds and even thousands of these lines:
... TCP localhost:43030->localhost:10303 (CLOSE_WAIT)
... TCP localhost:40982->localhost:10341 (CLOSE_WAIT)
... TCP localhost:48718->localhost:10304 (CLOSE_WAIT)
... TCP localhost:41655->localhost:10350 (CLOSE_WAIT)
... TCP localhost:41915->localhost:10310 (CLOSE_WAIT)
... TCP localhost:49746->localhost:10322 (ESTABLISHED)
I was researching why is this that way, but I don't understand, because I am calling curl_close and I would assume that CURL would close PROXY connection as well, or am I missing something?
Versions:
PHP 7.1.4-1+deb.sury.org~trusty+1 (cli) (built: Apr 11 2017 22:45:20) (NTS)
curl 7.52.1 (x86_64-pc-linux-gnu) libcurl/7.52.1 OpenSSL/1.0.1f zlib/1.2.8 libidn2/0.9 libpsl/0.11.0 (+libicu/52.1) libssh2/1.4.3 nghttp2/1.19.0 librtmp/2.3
I also came across this issue. The work around is add
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Connection: close"));
At first I thought is was related with php7, because i can reproduce it on a php7 box, but on php5, it will not happen. Unfortunately, I tried this on another php7 box, it also did not happen again. So I am still confused about the root cause.

IPN PayPal - Couldn't resolve host [duplicate]

I'm trying to fetch the contents of a page using CURL. The page that is doing the fetching is https and the page it is trying to fetch is also https. I'm getting an error "Couldn't resolve host" with all of the settings I try.
$c=curl_init();
curl_setopt($c, CURLOPT_URL,$url);
//curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
curl_setopt ($c, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYPEER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($c, CURLOPT_HEADER, FALSE);
$html=curl_exec($c);
if($html === false) {
echo curl_error($c);
}
else {
echo 'Operation completed without any errors';
}
curl_close($c);
Any ideas?
I found that curl can decide to use IPv6, in which case it tries to resolve but doesn't get an IPv6 answer (or something to that effect) and times out.
You can try the curl command line switch -4 to test this out:
curl -4 http://x.com
In PHP, you can configure this line by setting this:
curl_setopt($c, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Official manual page for this option: https://curl.se/libcurl/c/CURLOPT_IPRESOLVE.html
Maybe a DNS issue?
Try your URL against this code:
$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, 'YOUR_URL' );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h));
I had the same problem. Coudn't resolve google.com. There was a bug somewhere in php fpm, which i am using. Restarting php-fpm solved it for me.
Just a note which may be helpful- I was having this trouble with Apache on my laptop (which connects by wifi AFTER startup), and restarting the server (after connect) fixed the issue. I guess in my case this may be to do with apache starting offline and perhaps there noting that DNS lookups fail?
There is a current bug in glibc on Ubuntu which can have this effect:
https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733
To resolve it, update libc and all related (Packages that will be upgraded: libc-bin libc-dev-bin libc6 libc6-dev libfreetype6 libfreetype6-dev locales multiarch-support) and restart the server.
We need to add host security certificate to php.ini file. For local developement enviroment we can add cacert.pem in your local php.ini.
do phpinfo(); and file your php.ini path open and add uncomment ;curl.capath
curl.capath=path_of_your_cacert.pem
If you do it on Windows XAMPP/WAMP it probaly won't work as in my case.
I solved the problem setting up Laravel's Homestead/Vagrant solution to create my (Ubuntu) development environment - it has built-in: Nginx, PHP 5.6, PHP 7.3, PHP 7.2, PHP 7.1, MySQL, PostgreSQL, Redis, Memcached, Node... to name just a few.
See here for info how to set up the environment - it's really worth the effort!
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!
Then you can easily switch PHP versions or set up more virtual hosts, new databases just in seconds.
After tried all above, still can't resolved my issue yet.
But got new solution for my problem.
At server where you are going to make a request, there should be a entry of your virtual host.
sudo vim /etc/hosts
and insert
192.xxx.x.xx www.domain.com
The reason if you are making request from server to itself then, to resolve your virtual host or to identify it, server would need above stuff, otherwise server won't understand your requesting(origin) host.
Your getting the error because you're probably doing it on your Local server environment. You need to skip the certificates check when the cURL call is made. For that just add the following options
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
You may have to enable the HTTPS part:
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
And if you need to verify (authenticate yourself) you may need this too:
curl_setopt($c, CURLOPT_USERPWD, 'username:password');
add yourlocalhost
ex. 127.0.0.1 cards.localhost
in the /etc/hosts directory.
Now restart apache server

Define host and different request addr on curl

I want to automatize the installation of some software on a new host for domains whose DNS servers don't direct this domain to the desired server.
Is it possible to go through the installation process using cURL? I'd need to set the REQUEST_HOST and REQUEST_ADDR to 2 different things then.
Example:
I'd like to setup a wordpress blog gowordpress.tld on machine
123.456.789.1
The DNS record for gowordpress.tld is set to 987.654.321.1
The webserver on Host 123.456.789.1 is set to servername
.gowordpress.com (nginx)
I'd like to go through the steps using a PHP script on Host
123.456.789.1
I'd like to use cURL.
Any ideas?
You have to provide IP address in URI form and specify host as one of cURL option, with CURLOPT_HTTPHEADER:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://123.123.123.123/');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: gowordpress.tld') );
$res = curl_exec($ch);
curl_close($ch);
Or directly from command line you can do:
curl -H 'Host: gowordpress.tld' http://123.456.789.1/
To check only the response status and headers use the -I switch:
curl -I -H 'Host: gowordpress.tld' http://123.456.789.1/
Curl utility is available in all debian based systems: apt-get install curl

cURL script works when run from one server but not another

I recently moved a cURL script from one server (Ubuntu) to another (Mac OS X). The script worked when run from the Ubuntu server but it doesn't work on the OS X server. I get error 35 and this:
error:14077417:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert illegal parameter
Any ideas?
Edit: Here's what's probably a really good clue. This doesn't work:
curl -v https://foo.com/OA_HTML/RF.jsp?function_id=123
But this does:
curl -v -sslv3 https://foo.com/OA_HTML/RF.jsp?function_id=123
Now I just need to know how to do the equivalent of the second command from within PHP.
Could be some php.ini setting or apache follow or openbasedir setting !
I know I'm late but the equivalent of
curl -v -sslv3 https://foo.com/OA_HTML/RF.jsp?function_id=123
is
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSLVERSION, 3); //set to force SSL3
curl_setopt($ch, CURLOPT_URL, "https://foo.com/OA_HTML/RF.jsp?function_id=123");
$result = curl_exec ($ch); //execute the request
Most of the time CURL over SSL uses OpenSSL, more than likely either CURL or OpenSSL is not installed or enabled on the new server. Save a file on the server with:
<?php phpinfo(); ?>
and check if these are enabled
ADD
Either that or your SSL Certificates where not created on the machine you are trying to use them on. Each SSL certificate has specific information about each machine (if received from an online vendor)

Categories