-2

I'm trying to fetch data from the Internet using PHP. Being at work, I'm behind a password protected proxy which seems to cause trouble.

I have consulted many posts on StackOverflow and several other platforms, each one giving me a different solution, none of which could fill my needs.

var_dump(stream_get_wrappers());

$opts = array("http" => ['proxy'=>'user:password@webproxy.xxx.intra:####',
                        'request_fulluri' => True]
        );

stream_context_set_default($opts);

$homepage = file_get_contents("www.google.com");
echo $homepage;

I've added var_dump(stream_get_wrappers()); as advised in one of the posts I've previously read, returning : [https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip]

And the warning I get is the following :

PHP Warning: file_get_contents(www.google.com): failed to open stream: No such file or directory in /home/user/project/app/connector.php on line 15

Line 15 being $homepage = file_get_contents("www.google.com");

I've been stuck there for way too long, and any help is greatly appreciated.

Thank you.


EDIT : I have added "http://" to the beginning of the address, giving me this error :

PHP Warning: file_get_contents(google.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/project/app/connector.php on line 15

For some reason this error got displayed twice, the second one being almost the same with less information.

ilomax
  • 568
  • 1
  • 4
  • 24
  • 1
    `www.google.com` is actually a filename, which really doesn't exist. If you want to perform an HTTP request, make sure the URI starts with that scheme. – CodeCaster Nov 14 '17 at 09:41
  • I don't think www.google.com is a valid file name..? – Param Kumar Nov 14 '17 at 09:43
  • @CodeCaster what scheme ? http:// ? That's what I get in return : _PHP Warning: file_get_contents(http://www.google.com): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/user/project/app/connector.php on line 15_ – ilomax Nov 14 '17 at 09:45
  • @ThePlusProgrammer I'm new to PHP in general and I don't really know what I should do. I get that 'www.google.com' isn't a valid file, but how can I fetch the contents of the site then ? – ilomax Nov 14 '17 at 09:48
  • Yeah so that's a new error. What did your research for that error show? https://stackoverflow.com/questions/20064372/file-get-contents-php-network-getaddresses-getaddrinfo-failed-name-or-servi – CodeCaster Nov 14 '17 at 09:49
  • @ThePlusProgrammer it's a perfectly valid filename on all filesystems I know, barring FAT16. A file with that name just doesn't exist, hence the error. – CodeCaster Nov 14 '17 at 09:50
  • I think you are looking for web scraping thing like to get data from URL, If yes try this link it would help http://www.oooff.com/php-scripts/basic-php-scraped-data-parsing/basic-php-data-parsing.php – Param Kumar Nov 14 '17 at 10:03
  • I think you are looking for web scraping thing like to get data from URL, If yes try this [link](http://www.oooff.com/php-scripts/basic-php-scraped-data-parsing/basic-php-data-parsing.php) it would help – Param Kumar Nov 14 '17 at 10:04
  • @CodeCaster I was reading the same post you linked, but I really do not think I can do much by myself. I'll try and figure my way out with the tech support at work. Thanks for your time and help ! – ilomax Nov 14 '17 at 10:06
  • @ThePlusProgrammer actually, the main goal of these lines was just to test whether or not PHP is able to access the Internet. I'm working on something else which requires it. – ilomax Nov 14 '17 at 10:13

1 Answers1

0

After searching even more, here is what I have come up with :

$auth = base64_encode('login:password');

$opts = array(
    'http' => array(
        'proxy' => 'tcp://proxy_host:proxy_port',
        'request_fulluri' => true,
        'header' => "Proxy-Authorization: Basic $auth",
    ),
);
$context = stream_context_create($opts);

$file = file_get_contents("http://www.google.com", False, $context);
echo $file;

This seems to work fine. Thanks for your help and advices.

ilomax
  • 568
  • 1
  • 4
  • 24