PHP 5.3.6 - curl_exec hangs - php

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

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.

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).

php use of file_get_contents with https -

I looked at the threads and followed suggestions - which got me here...
I'm using WAMP - php rev 5.4.12 (Win7)
Code is as simple as possible:
$result = file_get_contents("https://g4apps.bliptrack.net/blipzones/report/publicdisplayapi.seam?display_id=dvp_vms4");
(this URL returns an XML file - works in browsers....)
The error is
"Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?..."
I did add to php.ini:
allow_url_include = On
extension=php_openssl.dll
to no avail
When I ask, I get: openssl: no http wrapper: yes https wrapper: no
Any suggestions? (I work w/PHP but not an expert...)
You should use cURL here instead of socket connection.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://g4apps.bliptrack.net/blipzones/report/publicdisplayapi.seam?display_id=dvp_vms4");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xmlPage = curl_exec($ch);
curl_close($ch);
notice use of "CURLOPT_SSL_VERIFYPEER" as 0/false.

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

Curl causes apache to seg fault

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

Categories