Jump to content

unexpected ':' from code that should work?


randall

Recommended Posts

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!

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}
?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.