PHP cURL not working - on subdomains
I have a PHP script that I am trying to get the content of a page. Using code im 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 great for me as it is now. The problem I am facing is that if I change the url of the string to
$url = "http://-test.tumblr.com";
or $url = "http://test-.tumblr.com";
It won't work. I understand that -test.example.com or test-.example.com are not valid hostnames, but with Tumblr they do exist. Is there a job for this?
I even tried to create a header redirect in another php file, so cURL will get the correct hostname first, but works the same.
thank
source to share
Domain names with hyphens
As you can see in the previous question about valid characters in a subdomain , is -
not a valid character to start or end a subdomain. So this is actually the correct behavior. The same issue was reported to curl
the mailing list a while ago, but since curl
it follows the standard, there is really nothing to change on their site.
Chances are tumblr is aware of this and therefore offers an alternative address leading to the same site.
Possible workaround
However, you can try using nslookup
to manually find an IP address and then send a request directly to that IP (and manually set the hostname to the correct value). I haven't tried this, but it seems nslookup
to be able to resolve malformatted domain names that start or end with a hyphen.
curl
Also, you should know that the php function curl
must be a direct interface to the command line tool curl
, so if you are faced with a particular behavior, it is likely due to the logic in the curl
command-line tool, not a php-function.
source to share