soma56 Posted June 17, 2010 Share Posted June 17, 2010 I'm playing around with cURL and not having much success. No errors, but rather - nothing at all. I found this simple cURL script that I've been working with in attempt to understand cURL better. <?php $Url = "http://www.mysimplehtmlsite.ca"; function DownloadUrl($Url){ // create a new curl resource $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 1); // set URL to download curl_setopt($ch, CURLOPT_URL, $Url); // set referer: curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/"); // user agent: curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); // remove header? 0 = yes, 1 = no curl_setopt($ch, CURLOPT_HEADER, 0); // should curl return or print the data? true = return, false = print curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 50); // download the given URL, and return output $output = curl_exec($ch); // close the curl resource, and free system resources curl_close($ch); // print output return $output; } DownloadUrl($Url); ?> After some reading I did find a couple of potential solutions: Someone suggested to ensure that cURL was installed - didn't work // is curl installed? if (!function_exists('curl_init')){ die('CURL is not installed!'); } I read somewhere else that return the output before closing the cURL resource? - didn't work // print output return $output; // close the curl resource, and free system resources curl_close($ch); I got desperate so I tried some illogical things - didn't work // print output return $output; echo $output; print $output; Finally I found a suggestion on checking to see if an error occurred - didn't work (no errors came back) // Check if any error occured if(!curl_errno($ch)) { $info = curl_getinfo($ch); echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url']; } I suspect this is a very simple fix if anyone has a second and can help me out. Link to comment https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/ Share on other sites More sharing options...
kenrbnsn Posted June 17, 2010 Share Posted June 17, 2010 You're returning the $output at the end of your function, but you're not doing anything with it. Try: <?php $Url = "http://www.mysimplehtmlsite.ca"; function DownloadUrl($Url){ // create a new curl resource $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 1); // set URL to download curl_setopt($ch, CURLOPT_URL, $Url); // set referer: curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com/"); // user agent: curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); // remove header? 0 = yes, 1 = no curl_setopt($ch, CURLOPT_HEADER, 0); // should curl return or print the data? true = return, false = print curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 50); // download the given URL, and return output $output = curl_exec($ch); // close the curl resource, and free system resources curl_close($ch); // print output return $output; } $page = DownloadUrl($Url); echo $page; ?> Ken Link to comment https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/#findComment-1073276 Share on other sites More sharing options...
soma56 Posted June 17, 2010 Author Share Posted June 17, 2010 Thanks Ken:) I decided to play with other scripts. Out of curiosity does anyone know what I'm missing here? It seems to parse however simply brings back 'Array': $url = "http://www.mysite.ca"; function get_web_page( $url ) { $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10, // stop after 10 redirects ); $ch = curl_init( $url ); curl_setopt_array( $ch, $options ); $content = curl_exec( $ch ); $err = curl_errno( $ch ); $errmsg = curl_error( $ch ); $header = curl_getinfo( $ch ); curl_close( $ch ); $header['errno'] = $err; $header['errmsg'] = $errmsg; $header['content'] = $content; return $header; } $contents = get_web_page($url); echo $contents; ?> Link to comment https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/#findComment-1073424 Share on other sites More sharing options...
kenrbnsn Posted June 17, 2010 Share Posted June 17, 2010 You're returning an array from the function and you can not just echo an array. You can do <?php $contents = get_web_page($url); echo '<pre>' . print_r($contents,true) . '</pre>'; ?> to see what's in the array. Ken Link to comment https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/#findComment-1073433 Share on other sites More sharing options...
soma56 Posted June 17, 2010 Author Share Posted June 17, 2010 Thanks Ken, I appreciate it. I'm still working on learning arrays and how they. I know it may have seemed like a very stupid question however consider that I though PHP was a bad ingredient in Chinese rice a month ago I think I've come along way. Link to comment https://forums.phpfreaks.com/topic/205015-curl-returning-blank-page/#findComment-1073513 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.