curl_exec does not return any data
I have an URL like this http://www.url.com/cakephp/controller/action/param:1
that produces an HTML page.
Inside a php file I have:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"http://www.url.com/cakephp/controller/action/开发者_Go百科param:1" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
$results=curl_exec($ch);
echo $results." <br />";
This piece of code doesn't write anything except <br />
?
If I use another site like www.google.com it will be loaded and show correctly. What is my problem?
Most likely CURL's failing somehow. Try this:
$results = curl_exec($ch);
if ($results === FALSE) {
die(curl_error($ch));
}
echo $results . "<br />";
curl returns false when it fails, so you need to test for that case.
The problem is the site does not dish out anything if you do not follow the location (it looks like it may redirect, given it is cake) and if you do not accept cookies.
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt($ch, CURLOPT_URL, "http://www.trofeocaressa.it/cakephp/matches/showMatchesBySeasonId/season_id:1" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
$results=curl_exec($ch);
echo $results." <br />";
That should get the results returned. For future references Curl Manual it has a list of what you can pass what it does etc. Try some of the main items, such as cookies, referers, user agents etc.
Maybe you should add error reporting like Marc said, and close the handler
(curl_close($ch);)
精彩评论