Jump to content

Weird CURL Problem


Samuz

Recommended Posts

This code works nice on my personal remote server and my local one.

 

But I need it on another remote server.

 

Here's code: 

function getVer($url) {
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_NOBODY, true);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($curl, CURLOPT_FILETIME, true);

    $result = curl_exec($curl);

    if ($result === false) {
        die(curl_error($curl));
    }
    
    $final = 1;

    $timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
    if ($timestamp != -1) { //otherwise unknown
        $final = $timestamp;
    }
    else {
        $final = 1;
    }
    
    $test = substr($url, -2, 3);
    if($test == 'ss') :
        return str_replace('.css', '-'.$final.'.css', $url);
    elseif($test == 'js') :
        return str_replace('.js', '-'.$final.'.js', $url);
    endif;
}


If I attempt to it on this url getVer('http://static.trainingdragon.co.uk/assets/css/style.css') it doesn't work and returns an error 'couldnt connect to host'.

 

I looked around and I was under the assumption this was cause the site was being blocked or something.

 

Anyone have a hunch what the problem could be?

 

But I can easily view that url in the browser with no problem

Edited by Samuz
Link to comment
Share on other sites

dalecosp: If they were turned off, he wouldn't have gotten the "could not connect" message.

 

samuz: you answered your own question: the remote host blocked the request from the server your script was running on. It's possible the server's IP just happened to get caught up in some other range blacklisted by some filter (bot/spam prevention) but it's more likely the server's IP does have a history of being used by spammers/botters.

Link to comment
Share on other sites

dalecosp: If they were turned off, he wouldn't have gotten the "could not connect" message.

 

samuz: you answered your own question: the remote host blocked the request from the server your script was running on. It's possible the server's IP just happened to get caught up in some other range blacklisted by some filter (bot/spam prevention) but it's more likely the server's IP does have a history of being used by spammers/botters.

Thanks for your reply.

 

The thing is the file i'm trying to access is under a subdomain of the site the script is running from which confuses me even more, so they are on the same server - I know this for sure.

 

And @dalecosp, the script works for other sites, just not this subdomain.

Link to comment
Share on other sites

I've encountered a small hand full of servers in the past that due to some kind of incorrect configuration have problem routing to themselves, you could be seeing something like that. If you have SSH access to the server, login and try grabbing the file with wget or the curl commandline tool. Check to make sure the domain resolves properly. Check the firewall rules to see if maybe there is a rule blocking the traffic.

Link to comment
Share on other sites

I've encountered a small hand full of servers in the past that due to some kind of incorrect configuration have problem routing to themselves, you could be seeing something like that. If you have SSH access to the server, login and try grabbing the file with wget or the curl commandline tool. Check to make sure the domain resolves properly. Check the firewall rules to see if maybe there is a rule blocking the traffic.

I've also encountered the same problem and like what kicken did, I checked the firewall. Found out that it was just the firewall blocking the traffic.

Link to comment
Share on other sites

Not according nmap, port 22, 80 are opened from an external network.

Also, I tried to run the script through bash shell and got a correct result.

The problem is inside your php script, especially this line - curl_setopt($ch, CURLOPT_NOBODY, true);

Never used "cURL" in php but try to run next script that I created for you:

<?php

$url = 'http://static.trainingdragon.co.uk/assets/css/style.css';
 
// Create a curl handle to a non-existing location
$ch = curl_init($url);

//curl_setopt($ch, CURLOPT_NOBODY, true);

curl_setopt($ch, CURLOPT_FILETIME, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$out = curl_exec($ch);

if($out === false)
{
    echo 'Curl error: ' . curl_error($ch);
} else {
    
$fp = fopen('css/style.css', 'w');

fwrite($fp, $out);

}
// Close handle
curl_close($ch);
Link to comment
Share on other sites

No firewall rules have been set as far as I can see in cpanel.

 

Also i'm going to contact the host to enable SSH for us and see how it goes from there.

Not according nmap, port 22, 80 are opened from an external network.

Also, I tried to run the script through bash shell and got a correct result.

The problem is inside your php script, especially this line - curl_setopt($ch, CURLOPT_NOBODY, true);

Never used "cURL" in php but try to run next script that I created for you:

<?php

$url = 'http://static.trainingdragon.co.uk/assets/css/style.css';
 
// Create a curl handle to a non-existing location
$ch = curl_init($url);

//curl_setopt($ch, CURLOPT_NOBODY, true);

curl_setopt($ch, CURLOPT_FILETIME, true);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$out = curl_exec($ch);

if($out === false)
{
    echo 'Curl error: ' . curl_error($ch);
} else {
    
$fp = fopen('css/style.css', 'w');

fwrite($fp, $out);

}
// Close handle
curl_close($ch);

 

And I received the same cURL here: (couldn't connect to host)

Link to comment
Share on other sites

When they enabled a SSH connection for you try to run your php script. If the problem is still there, you would run as @kicken said above  cURL by shell terminal. 

It would be someting like:

curl  --verbose --output 'remote_style.css' 'http://static.trainingdragon.co.uk/assets/css/style.css'
Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.