Curl causes apache to seg fault - php

I'm a bit stuck, curl causes segmentation faults in apache and I can't find out why.
I made a simple case, trying to understand :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://google.fr');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT,30);
curl_exec($ch);
My conf, apache2, php5.3, ubuntu 11.10. Tried to reinstall php5-curl with no success
If you have ever seen this before, and have an idea please tell me !

Have you tried adding curl_close($ch); as last line in your example?
Still, it shouldn't segfault ;)
Try running the code manually like this: php test.php

Related

Curl: Showing "Can't resolve domain name" error of the all localhost sites from PHP

Today my cURL is starting to show me this error Could not resolve: {local-domain.test} (domain name not found)
All working fine if I'm trying to get some info from an external link, but local sites aren't working properly.
So, here is my code
public function send($type, $url, $data)
{
$type = strtolower($type);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($type == 'post') {
$urlWithData = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $urlWithData);
}
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$out = new \stdClass();
$out->status = $status;
$out->body = json_decode($result, true);
return $out;
}
All start to work if I will add that host into my /etc/hosts/ file but I have a huge list of the domain and before updating PHP
all worked fine on my side. I'm using dnsmasq as a dynamic host resolving tool.
OS: MacOs High Siera
Apache: 2.4.37
PHP: Multiple PHP versions, from 5.6-7.3
Some debug info:
Ping to that domain from the terminal is working fine (showing localhost)
Everything is working fine even If I run curl -v local-domain.test from a terminal
I've tried to use CURLOPT_DNS_USE_GLOBAL_CACHEoption.
Cleared all DNS cache of my machine from a terminal
Maybe I forgot to add something to the php.ini of each PHP? Also, all worked fine before I've made updating process of them all.
This answer on this thread worked for me.
Running:
brew uninstall curl-openssl --ignore-dependencies
(removes the last version of curl-openssl, ensuring parity between curl versions used by your system and PHP)
... then followed by a valet restart
... allowed local curl to resolve Valet hostnames properly.

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

MAMP php 5.6.2 file_get_contents not working (timeout)

I have searched and cannot find a reason why this isn't working. I have tried executing from command line as well as apache and it does not work.
<?php
echo file_get_contents("http://google.com");
?>
I have a simple php script as shown above that simply makes a request to google and outputs the contents.
Error:
Warning: file_get_contents(http://google.com): failed to open stream: Operation timed out in /Applications/MAMP/htdocs/p.php on line 2
PHP version:
PHP 5.6.2 (cli) (built: Oct 20 2014 16:21:27)
allow_url_fopen = On
The odd part is if I use /usr/bin/php it works fine. (Built in apple version)
Here is the wireshark capture:
https://www.dropbox.com/s/u7jdg494fmud22i/packet_capture_php_network.pcapng?dl=0
I suggest you to use a CURL alternative:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Your apache server might have restrictions on it. Check out this post which details the fix. Should just be a config change in your php.ini file (make sure you change the MAMP PHP's php.ini file).

Curl doesn't get external websites

I have some troubles with cUrl configuration on my local machine (my own computer).
I can only download site that is placed on localhost. Trying any other host causes failure (empty string returned). I'm sure that code is ok - it works on my production server.
Also, curl_errno doesn't return any error.
I can't find the problem, please help.
Edit: Here's the code.
<?php
$url = "http://stackoverflow.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch); // It's empty
is PHP curl installed on your machine, if not then install it first and then try
for installation you may use either of these methods
http://php.net/manual/en/curl.installation.php
sudo apt-get install php5-curl - and then restart apache.
it should work
you may want to try this instead of curl
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$url = "http://stackoverflow.com";
echo file_get_contents($url);
also make sure that allow_url_fopen is turned on in php.ini
http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Echo the result ! [Tested and works]
<?php
$url = "http://stackoverflow.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;//Added

PHP 5.3.6 - curl_exec hangs

I am having a odd problem with the php code below. I have noticed the issue is randomly happened in PHP 5.3.6. It works every times when I run it in PHP 5.2.5. Unfortunately we can downgrade the php due to other OCIs issues with 5.2.5, so we have to use 5.3.6. When the issue is occurred, I don't get any response back at all and it happens randomly. Please help, I need to get this project done asap. Thanks.
<?php
$url = 'https://www.PayEverywhere.com/api/vtapi.aspx?profile_id=XXXXXXXX&profile_key=XXXXXXXXXXXXX&transaction_type=S&card_number=...';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_FRESH_CONNECT,TRUE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
if ( ! $response = curl_exec($ch) )
{
echo "Error " . curl_error($ch). "\n";
}
echo $response;
curl_close ($ch);
?>
After having retrieved the following archives
http://curl.haxx.se/download.html
http://www.openssl.org/source/
http://web.mit.edu/kerberos/dist/index.html
# Install Curl
./configure --prefix=/curl/prefix --with-ssl=/ssl/prefix --disable-ipv6
# As curl depends on OpenSSL, install OpenSSL
./configure --prefix=/openssl/prefix
# As OpenSSL depends on Kerberos, install Kerberos
./configure --prefix=/kerberos/prefix

Categories