randall Posted March 8, 2012 Share Posted March 8, 2012 Hello I am getting the following error when accessing this page... test.php <?php $url = "http://www.howtogeek.com"; $str = file_get_contents($url); function get_url_contents($url){ $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); return $ret; } ?> Easy right??? I am getting the following error. Parse error: syntax error, unexpected ':' in /home/username/public_html/tools/crawler/test.php on line 2 The above example code was taken from How To Geek. http://www.howtogeek.com/howto/programming/php-get-the-contents-of-a-web-page-rss-feed-or-xml-file-into-a-string-variable/ I have tried single quotes, removed the http:// part, a few other small things but I just don't understand. Can anyone shed some light on it? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/ Share on other sites More sharing options...
samshel Posted March 8, 2012 Share Posted March 8, 2012 Is this is all the code in your file? Also try removing line 2 completely and typing by hand if you had copy pasted it. If you are including any other file before this code, there might be an open double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325378 Share on other sites More sharing options...
randall Posted March 8, 2012 Author Share Posted March 8, 2012 Samshel That is the only code in the test.php page and I call it directly. I have changed line 2, the quotes were not formatted properly... but... I am only getting a blank page now. It should output a list of urls should it not? Could it be something with the server setup? I will pm you the url. Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325384 Share on other sites More sharing options...
randall Posted March 8, 2012 Author Share Posted March 8, 2012 Here is the code that is now working... but I am now getting a blank page. <?php $url = "http://www.howtogeek.com"; $str = file_get_contents($url); function get_url_contents($url){ $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); return $ret; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325387 Share on other sites More sharing options...
kicken Posted March 8, 2012 Share Posted March 8, 2012 That would be because your not echoing anything. Also you're calling file_get_contents which is php's built in function, not your get_url_contents function. As such, you could just remove that whole function as your not using it. echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325395 Share on other sites More sharing options...
trq Posted March 8, 2012 Share Posted March 8, 2012 You never actually call the function, or echo any results. Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325396 Share on other sites More sharing options...
randall Posted March 9, 2012 Author Share Posted March 9, 2012 ahhh... well that was really stupid of me!!! Anyway... So far I am getting good results with the code I now have but I want to go further... <?php $url = (isset($_GET['url']) ? $_GET['url'] : 0); $str = file_get_contents($url); function get_url_contents($url){ $crl = curl_init(); $timeout = 5; curl_setopt ($crl, CURLOPT_URL,$url); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); $ret = curl_exec($crl); curl_close($crl); return $ret; } echo trim(strip_tags(strtolower(html_entity_decode($str)))); ?> This is the output of the code when it is run using a variable in the url, I used cooking.com, "test2.php?url=http://www.cooking.com" as you can see there is quite a bit of left over script and css etc... I want to rip everything out of there except for the main text and replace some characters with a character of my choice. Numbers, brackets, dashes, etc... just words. cooking cookware, bakeware, cutlery, recipes and recipe ideas - cooking.com shop values gifts recurringsubscription recipes community cart | track order | help shop for: bakeware | cookware | cooking tools | cutlery | dinnerware | storage | small appliances free newsletters | buying guides | my account | best brands | contact us | stores | st. patrick's day contest if(typeof(cachebuster) == "undefined"){var cachebuster = math.floor(math.random()*10000000000)} if(typeof(dcopt) == "undefined"){var dcopt = "dcopt=ist;"} else {var dcopt = ""} if(typeof(tile) == "undefined"){var tile = 1} else {tile++} var gajshost = (("https:" == document.location.protocol) ? "https://" : "http://"); document.write(' bakeware set bar blender canister set coffee maker cookware set cutlery set cutting board deep fryer dinnerware set dutch oven espresso machine food processor ice cream maker rachael ray rice cooker roaster slow cooker stand mixer teakettle toaster oven browse: recipes | special values shopping navigation new arrivals top rated schedule to save our biggest savings made in the usa le creuset bakeware deals shop all savings on carving knives Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325449 Share on other sites More sharing options...
trq Posted March 9, 2012 Share Posted March 9, 2012 That question belongs in a completely different topic. ps; You still haven't removed or used that function. Quote Link to comment https://forums.phpfreaks.com/topic/258559-unexpected-from-code-that-should-work/#findComment-1325474 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.