14 Replies - 507 Views - Last Post: 04 January 2018 - 11:04 AM Rate Topic: -----

#1 Yhym  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-December 13

file get contents & cURL not displaying anything

Posted 18 December 2017 - 03:39 AM

I checked and it looks like cURL is enabled inside the php.ini file (I'm using XAMPP) and yet it's still not displaying anything. Same thing goes for file_get_contents. I simply get a blank web page as a result... Somebody has already tested this for me on their local network and it works so I'm really lost right now.

<?php
$auth = base64_encode("04d2ac7f76a0fbc0eee9dc5ef96b9259:dc70ffc7ad911236bc2e0822855e2d42");
$context = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$homepage = file_get_contents("https://api.intrinio...?identifier=AA", false, $context );


$login = '04d2ac7f76a0fbc0eee9dc5ef96b9259';
$password = 'dc70ffc7ad911236bc2e0822855e2d42';
$url = 'https://api.intrinio...?identifier=AA';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
echo($result);
?>


Anyone has an idea why? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: file get contents & cURL not displaying anything

#2 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6442
  • View blog
  • Posts: 26,059
  • Joined: 12-December 12

Re: file get contents & cURL not displaying anything

Posted 18 December 2017 - 04:50 AM

I would first configure PHP to display all errors:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


Then curl_exec may not return an explicit TRUE/FALSE value, see the docs, so I would check for a successful result, if (curl_exec($ch)) { echo 'success!'; }.

Question moved to PHP forum (and PHP tag removed from title).
Was This Post Helpful? 1
  • +
  • -

#3 Radius Nightly  Icon User is offline

  • D.I.C Head

Reputation: 19
  • Posts: 201
  • Joined: 07-May 15

Re: file get contents & cURL not displaying anything

Posted 18 December 2017 - 08:27 AM

I think curl_exec are disabled by default on global level on host, because its considered as a threat. I would try to enable it as is, not in php site or htdocs.
Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1864
  • View blog
  • Posts: 5,829
  • Joined: 15-January 14

Re: file get contents & cURL not displaying anything

Posted 18 December 2017 - 10:08 AM

I don't see any indication in the manual that it's disabled by default. But, if it is disabled, then that will cause a fatal error so as long as error messages are being displayed in either the browser or a log file that should be obvious.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3721
  • View blog
  • Posts: 13,512
  • Joined: 08-August 08

Re: file get contents & cURL not displaying anything

Posted 18 December 2017 - 04:35 PM

I'd guess that the server is blocking file_get_contents(), but I don't see you using the result.
Was This Post Helpful? 0
  • +
  • -

#6 Radius Nightly  Icon User is offline

  • D.I.C Head

Reputation: 19
  • Posts: 201
  • Joined: 07-May 15

Re: file get contents & cURL not displaying anything

Posted 18 December 2017 - 08:14 PM

As far as i know, its usually disabled for security reasons, including some other curl_#functions, and because he is using XAMPP (who by default has disabled some curl_#functions), he should check php.ini (XAMPP got several of them, check them all for php_curl.dll) to enable it, check disabled functions inside (disable_functions), and then check it with phpinfo() to validate it.

Its just what i would do first. I would not trust someone elses PC, specially if me and myself didnt check it (had that kind of experience, friend said, its working-its working, but actually not).

If file_get_contents works local, but not online, maybe firewall are blocking it. Check your port 80/443 (Skype?).

BTW Its also possible your hosting doesnt know his domain (if you are hosting by yourself, this message are for Windows). If its working online for other peoples, its because they are accessing your domain from DNS, and if its not working for you at host machine, it means he is confused accessing the domain that leads back to him. You have to set host domain-IP at your PC in order to be able to access domain at your PC who are the host.
For example if host PC accessing localhost, maybe 127.0.0.1 or eg. 192.168.0.99 (his internal IP), he will be able to access the site he host. But if site depends on a domain, like eg. mysite.net, host machine/system doesnt know thats him, so asking for file_get_contents at mysite.net will probably return nothing (domain doesnt exist for him, leads back to him, return nothing again, or it leads to router, DMZ or so who will block it to break the loop he is going to start). In system32, drivers, etc, hosts you should add hosts internal IP and bind it to the domain (for this example, add new line: 192.168.0.99 mysite.net), so when he want to access the domain, he will know thats him.

This post has been edited by Radius Nightly: 18 December 2017 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 Yhym  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-December 13

Re: file get contents & cURL not displaying anything

Posted 19 December 2017 - 07:16 AM

I enabled sockets and made sure curl is enabled too in the .ini file. Still nothing.
Also, tried this:
echo 'Curl: ', function_exists('curl_version') ? 'Enabled' : 'Disabled';

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);


And it shows "Curl: Enabled". I've tried the error reporting feature too and it doesn't display anything either. Unless I'm doing something wrong. Anything else I should try? I'm essentially just trying to connect from a localhost to Intrinio's API. It doesn't necessarily have to work with that curl function or 'file get contents' if there is a different way...
Thanks.

This post has been edited by andrewsw: 19 December 2017 - 07:38 AM
Reason for edit:: removed previous quote, just use the REPLY button

Was This Post Helpful? 0
  • +
  • -

#8 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2363
  • View blog
  • Posts: 9,485
  • Joined: 03-December 12

Re: file get contents & cURL not displaying anything

Posted 19 December 2017 - 08:58 AM

Is exception reporting turned on?

At a minimum, you should be getting a status code, if the request worked and the issue is on the other side.

Taking your initial code:

<?php
$auth = base64_encode("04d2ac7f76a0fbc0eee9dc5ef96b9259:dc70ffc7ad911236bc2e0822855e2d42");
$context = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$homepage = file_get_contents("https://api.intrinio...?identifier=AA", false, $context );


$login = '04d2ac7f76a0fbc0eee9dc5ef96b9259';
$password = 'dc70ffc7ad911236bc2e0822855e2d42';
$url = 'https://api.intrinio...?identifier=AA';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
?>


Was This Post Helpful? 0
  • +
  • -

#9 Yhym  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-December 13

Re: file get contents & cURL not displaying anything

Posted 19 December 2017 - 12:38 PM

Not sure if that's what you wanted me to do but when I run this:

<?php
$auth = base64_encode("04d2ac7f76a0fbc0eee9dc5ef96b9259:dc70ffc7ad911236bc2e0822855e2d42");
$context = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$homepage = file_get_contents("https://api.intrinio.com/companies?identifier=AA", false, $context );

$login = '04d2ac7f76a0fbc0eee9dc5ef96b9259';
$password = 'dc70ffc7ad911236bc2e0822855e2d42';
$url = 'https://api.intrinio.com/companies?identifier=AA';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
$result = curl_exec($ch);
curl_close($ch);  
print_r($result);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>


Nothing shows up.

This post has been edited by andrewsw: 19 December 2017 - 01:03 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2363
  • View blog
  • Posts: 9,485
  • Joined: 03-December 12

Re: file get contents & cURL not displaying anything

Posted 19 December 2017 - 12:44 PM

Have you ever used a tool like Postman?

Try using that first to ensure routes and everything are correct. It is my primary tool, because I spend 90% of my time creating API's and services.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6442
  • View blog
  • Posts: 26,059
  • Joined: 12-December 12

Re: file get contents & cURL not displaying anything

Posted 19 December 2017 - 01:04 PM

@OP You do not have to quote the previous post, use the Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#12 Yhym  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 08-December 13

Re: file get contents & cURL not displaying anything

Posted 20 December 2017 - 04:52 AM

Ok, sorry Andrew.

Solution:
$auth = base64_encode("04d2ac7f76a0fbc0eee9dc5ef96b9259:dc70ffc7ad911236bc2e0822855e2d42");
$context = stream_context_create(['http' => ['header' => "Authorization: Basic $auth"]]);
$homepage = file_get_contents("https://api.intrinio.com/companies?identifier=AA", false, $context );
$jsonary = json_decode($homepage,TRUE);
print_r($jsonary);

That said, curl still isn't working so if someone has an idea on how to fix it then I'd still appreciate if you could let me know.
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2363
  • View blog
  • Posts: 9,485
  • Joined: 03-December 12

Re: file get contents & cURL not displaying anything

Posted 20 December 2017 - 08:53 AM

When I get home, I can post a communications class that handles cURL requests for you to use. Until then, I don't have access to it from work.
Was This Post Helpful? 1
  • +
  • -

#14 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2363
  • View blog
  • Posts: 9,485
  • Joined: 03-December 12

Re: file get contents & cURL not displaying anything

Posted 28 December 2017 - 09:04 PM

Took a bit, but I found the file!


<?php

class Communication
{

    const MAILGUN_API = 'key-###########################';
    const DOMAIN = '###############';

    public static function Call($to)
    {
        $config = array();

        $config['api_key'] = self::MAILGUN_API;

        $config['api_url'] = "https://api.mailgun.net/v3/" . self::DOMAIN . "/messages";

        $message = array();

        $message['from'] = "[email protected]" . self::DOMAIN;

        $message['to'] = $to;

        $message['h:Reply-To'] = $to;

        $message['subject'] = "Visitor Spotted";

        $message['html'] = file_get_contents('/home/stoneci2/public_html/api/src/public/templates/mail_template.html');

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $config['api_url']);

        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

        curl_setopt($ch, CURLOPT_USERPWD, "api:{$config['api_key']}");

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

        curl_setopt($ch, CURLOPT_POST, true);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $message);

        $result = curl_exec($ch);

        curl_close($ch);

        return $result;
    }



}


Was This Post Helpful? 0
  • +
  • -

#15 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1864
  • View blog
  • Posts: 5,829
  • Joined: 15-January 14

Re: file get contents & cURL not displaying anything

Posted 04 January 2018 - 11:04 AM

Quote

As far as i know, its usually disabled for security reasons, including some other curl_#functions, and because he is using XAMPP (who by default has disabled some curl_#functions), he should check php.ini (XAMPP got several of them, check them all for php_curl.dll) to enable it, check disabled functions inside (disable_functions), and then check it with phpinfo() to validate it.

I don't think any of that is accurate. I don't know what you mean by file_get_contents is "usually" disabled, it's not disabled by default so why would you say it's "usually" disabled? Are you talking about shared hosts? They would only completely disable that function if they are bad at server security, in which case you shouldn't use them at all. If it IS disabled, then you will get a fatal error message when trying to use it, you don't have to guess if it's enabled or not. If the fopen wrappers for streams are disabled, and you use file_get_contents to open a remote file, again, you will get an error message saying what the problem is and, again, you don't have to guess. XAMPP does not disable any functions or classes, at least not in the ini file I see in my installation of XAMPP, so I don't know why you're suggesting it does. CURL doesn't have a lot of functions, you usually only need curl_init, curl_setopt, and curl_exec. And, again, if any of those are disabled you will get an error message, you do not have to guess.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1