0

Here's my setup:

$url1 = "www.gyngen.dk";
$url2 = "gyngen.dk";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);  
$result = curl_exec($curl);
curl_close($curl);

Using $url1 will result in an empty string and $url2 will work as intended. But why my fellow stackoverflowians, is cURL so delicate when it comes to the presence of ‘www.’ , when it (in my experience) doesn’t matter when using URLs in my browser (firefox).

Thanks in advance!

castis
  • 8,154
  • 4
  • 41
  • 63
Rene Jorgensen
  • 169
  • 1
  • 8
  • 2
    Issue 1, wrong quotes. – Jonnix Jun 13 '16 at 15:31
  • 1
    Browsers are designed to be used by people who couldn't care less about technology but curl is a tool for programmers: you're expected to be able to format a URL properly. Browsers will often Google it for you, something you really don't want in your scripts. – Álvaro González Jun 13 '16 at 15:32
  • Why are people downvoting my question? I've got some useful answers, so I'm fully satisfied, but I'm a bit curious as to how I should have asked my question differently? – Rene Jorgensen Jun 13 '16 at 15:44
  • The downvote button hover message starts with "This question does not show any research effort;" I understand the issue in your question is confusing but enough googling would have put you at the right answer eventually. – castis Jun 13 '16 at 15:51
  • As a newcomer to programming I will most likely be able to find all my answers by googling enough (and mostly I do). If stackoverflow to some extent(!) isn’t meant as a convenient way to avoid those hours of googling, I don’t really see the purpose of this site. Again, I got my question answered perfectly, so it’s just my poor little ego that’s a little sad with the downvotes. – Rene Jorgensen Jun 13 '16 at 16:08
  • 1
    You know, that _is_ what this site is for. I'm glad you were able to find the answer you were looking for. – castis Jun 14 '16 at 02:23

3 Answers3

3

cURL does not follows redirects and going to www.gyngen.dk would redirect you to gyngen.dk using redirect. And since there is no redirect cURL receives empty web content.

Justinas
  • 41,402
  • 5
  • 66
  • 96
2

http://www.gyngen.dk redirects to http://gyngen.dk.

Your browser follows the redirect transparently.

PHP/cURL, with your settings, does not. This question explains how to change that.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

The www. and bare domains are two separate domains. Web browsers will sometimes try and obscure this difference for people who do not know any better, and if a page is not found at one of the addresses, it will sometimes try the other. In reality, the domains are distinctly their own, and curl does not make any attempt to hide this.

MrSalmon
  • 89
  • 1
  • 5
  • 1
    It will never `try for another`. You must configure your site to be accessible via `www.` subdomain. – Justinas Jun 13 '16 at 15:43
  • Not my downvote but it's most likely related to the part where you said "if a page is not found at one of the addresses, it will sometimes try the other". Browsers wont just try other addresses if a 404 is given. In most cases, [one of the redirect response codes](https://en.wikipedia.org/wiki/URL_redirection#HTTP_status_codes_3xx) is needed as a response from a server for the browser to switch urls on you. – castis Jun 13 '16 at 15:45
  • While there is some special casing in some browsers for handling of domains that don't resolve via DNS in some circumstances (usually falling back to a search engine), that definitely is not what is happening here. – Quentin Jun 13 '16 at 16:01
  • There aren't different domains either. They are different hostnames, but they share a domain. – Quentin Jun 13 '16 at 16:02