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).
Related
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.
I am trying to run .php scripts on windows machine. I first tried older version of php which was php 5.4.6 and enabled php_curl on php.ini(found in php folder) and after running my script using cmd(c:\php\php.exe test.php) (i got this error:
php startup:unable to load dynamic library c:php\php_curl.dll' - The specified module could not be found
so i renamed c:/php to c:/phpOld/ and created another folder (c:/php) unzipped the newer version of php files in to it(php-5.6.17-nts-Win32-VC11-x86.zip).
I found two php.ini files on same folder as php files called php.ini-development and php.ini-production . I enabled php_curl on both of those files(changed ;extension=php_curl.dll to extension=php_curl.dll) and after running the script i still get this error:
Fatal error: Call to undefined function curl_init() in
I went and checked ext folder and I saw php_curl.dll file is there! could any one tell me why php curl is not running and I get the above error ?Thanks in advance.
Edit: this script runs correctly on webserver but gives empty result when running it locally! Is there any limitation on making curl post request this way ?
$data = array(
'a' => 'test1',
'b' => 'test2',
);
$url = "https://api.somewebsite.com/auth.aspx";
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$response = json_decode($json_response, true);
echo "full Response:".$json_response;
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.
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
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