cUrl无法从同一服务器获取文件

I am trying to fetch file from the same server i am running my php script in which i am cUrl to fetch it.

It does not download file and get timeout. I am able to get the file using same url from browser.

cUrl is able to get the file if the url is anything other than the same server.

Are their any settings i need to modify to support file download using cUrl on same server.

Appreciate your help here.

My code:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $file_url);
    $content = curl_exec($ch);
    curl_close($ch);

try this with all parameters of curl

function curl_download($Url){

// is cURL installed yet?
if (!function_exists('curl_init')){
    die('Sorry cURL is not installed!');
}

// create a new cURL resource handle
$ch = curl_init();

// Now set some options (most are optional)

// Set URL to download
curl_setopt($ch, CURLOPT_URL, $Url);

// Set a referer
//curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm");

// User agent
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");

// Include header in result? (0 = yes, 1 = no)
curl_setopt($ch, CURLOPT_HEADER, 0);

// Should cURL return or print out the data? (true = return, false = print)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Timeout in seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

// Download the given URL, and return output
$output = curl_exec($ch);

// Close the cURL resource, and free system resources
curl_close($ch);

return $output;
}

Use Google Chrome' Copy as cURL (open "dev tools", "Network" tab, right-click on a request) on a successfully fetched file, paste to a terminal, then execute.

If everything is fine, and you are able to get a file via curl at terminal, then there is something with headers and/or request params. If not, it's more likely that something wrong with your network configuration.

Also, check web server logs, do you even get to a web server with your script?

Are you using 127.0.0.1 or localhost or full TLD? Is browser connecting to Internet via proxy?

The workaround I found is this: Test if you are on your own server, then use a simple include for the same script:

    if (($ser = servername()) != $floc) {
       return(GetFileContent("$floc/$page.php"));
    } else { include "$page.php"; }