3

I have a PHP script that I'm trying to get the contents of a page. The code im using is below

$url = "http://test.tumblr.com";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$txt = curl_exec($ch);
curl_close($ch);

echo "$txt";

It works fine for me as it is now. The problem I'm having is, if I change the string URL to

$url = "http://-test.tumblr.com"; or $url = "http://test-.tumblr.com";

It will not work. I understand that -test.example.com or test-.example.com is not a valid hostnames but with Tumblr they do exists. Is there a work around for this?

I even tried creating a header redirect on another php file so cURL would be first getting a valid hostname but works the same way.

Thank you

chillers
  • 1,447
  • 2
  • 15
  • 25

1 Answers1

0

Domain Names with hyphens

As you can see in a previous question about the allowed characters in a subdomain, - is not a valid character to start or end a subdomain with. So this is actually correct behavior. The same problem was reported over the curl mailing list some time ago but since curl follows the standard, there is actually nothing to change on their site.

Most likely tumblr knows about this and therefore offers some alternative address leading to the same site.

Possible workaround

However you could try using nslookup to manually lookup the IP and then send your request directly to this IP (and manually setting the hostname to the correct value). I didn't try this out, but it seems as if nslookup is capable to resolve malformatted domain names that start or end in a hyphen.

curl

Additionally you should know, that the php curl function should be a direct interface to the curl command line tool and therefore, if you would encounter special behavior it would most likely be due to the logic in the curl command line tool and not the php function.

Community
  • 1
  • 1
s1lence
  • 2,188
  • 2
  • 16
  • 34
  • They do not offer any work around except to see if the blog exists. I'm trying to find a work around. :) – chillers Jan 24 '13 at 10:30
  • I've updated the answer with a possible workaround. I didn't try it out but I think it could work. – s1lence Jan 24 '13 at 16:09