Click here to Skip to main content
15,867,749 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hello Everyone;

I am having a php file that gets the contents from a URL, i am getting the failure message

Warning: file_get_contents(http://xxxxxx): failed to open stream: HTTP request failed! in xxxx.php on line xx


i tried so many online solutions but its still not working. here is the code

$cryptpass = rawurlencode(crypt($pc['pcpassword']));
$url = "http://" . $pc['pcname']."/Reports/ReportList.php?&username={$pc['pcusername']}&cryptpass=$cryptpass&noredir=1";
$parsed_list = read_general_list($url, false);


C#
function read_general_list($url, $make_assoc = false)
    {
        $compressed_data = file_get_contents($url);
    }




$compressed_data is always null and it throws an error:

Warning: file_get_contents(http://xxxxxx): failed to open stream: HTTP request failed! in xxxx.php on line xx

Any suggestions please?
Posted

As the error message says, the stream (URL) requested cannot be opened. There are many possible reasons for this:
1. base URL is bad. $pc['pcname']
2. username and/or password are bad
3. username/password do not have permission on the server
4. Your system cannot reach the server (firewall, PHP permissions, ...)
4. ...

I would use the following strategy to debug:
1. Dump $url and write it down.
2. Use a browser with debug tools (eg Firefox/Firebug) and try to access that URL.
3. Look at the headers returned to see what error the server reports (if any).
4. Think about why that error is returned...

Cheers,
Peter

If this answers your question, vote and mark it accepted.
 
Share this answer
 
Comments
amarasat 22-Mar-11 10:47am    
You are right, the URL seems fine, now its working, seems like this is not the main problem.

I have a URL, that parses metatags from the reprots in a folder and display them. This is working fine, but if open the windows debugger, and attach the process to the Apache.exe, the URL hasn't finished loading up and the Windows debugger throws an error

(24c.294): Stack overflow - code c00000fd (first chance)
First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
eax=0528db40 ebx=0117f158 ecx=00000007 edx=011b5770 esi=00000007 edi=0528ddb0
eip=007b93a9 esp=01513000 ebp=00000001 iopl=0 nv up ei pl nz na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010202
*** WARNING: Unable to verify checksum for c:\php\php5ts.dll
*** ERROR: Symbol file could not be found. Defaulted to export symbols for c:\php\php5ts.dll -
php5ts!zend_hash_quick_add_or_update+0x9:
007b93a9 57 push edi

If i launch the URL without windows debugger its working fine, if i have the windows debugger its throwing the error. Any suggestions what this could be.
amarasat 28-Mar-11 10:32am    
file_get_contents_curl worked like a charm, Thanks everyone
Motherangel 22-Feb-13 7:00am    
@amarasat,

Please could kindly rewrite the code to use the file_get_contents_curl. I'm having the error failed to open stream and I need to adopt your solution. Here is the code:

global $_english;

$query= ($_english ? 'http://lawrencehartntv2projcet.org/cgi-bin/ntv2_geo2_e.cgi' : 'http://lawrencehartntv2projcet.org/cgi-bin/ntv2_geo2_f.cgi').
'?' . PAR_DIR .'=' . $dir .
'&' . PAR_LATDEG .'=' . $latdeg .
'&' . PAR_LATMIN .'=' . $latmin .
'&' . PAR_LATSEC .'=' . $latsec .
'&' . PAR_LONGDEG .'=' . $longdeg .
'&' . PAR_LONGMIN .'=' . $longmin .
'&' . PAR_LONGSEC .'=' . $longsec;

$response=file($query);

foreach ($response as $value)
{
echo $value;
}
}
?>

This line of code $response=file($query); is retuning the above error. Please help.
I am sorry, i never updated this question completely. May be if some one is still looking for an answer.

This has worked for me. This is the equivalent function for File_get_contents, but can handle large amount of data. I found this solution online.

function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900