SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem using file_get_contents() in a cron

    Hi there,

    I've written a cron script, part of which uses the TinyURL API to return a shortened URL. My script is returning false on this line:

    Code:
    $short_url = file_get_contents( 'tinyurl/api-create?url='. $long_url );
    (the 'tinyurl/api-create' part above has been modified because the forum wouldn't let me post the full URL to the API as it thought I was spamming!)

    The script works perfectly when run in the browser, but not when run as a cron job on the server. Does anyone know an alternative way of doing this which will work when run on a server?

    Thanks in advance for any help you can give me.

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use a full directory path to the file rather than a relative path.

    When run via Apache, the script will be executing as though in the /usr/local/apache/htdocs directory (or equivalent). What directory does the script think it's executing in when run via cron?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, sorry I wasn't very clear in my original post. The full line which returning false is this:

    Code:
    $short_url = file_get_contents( 'http://tinyurl.com/api-create.php?url='. $long_url );
    So as you can see, I'm actually calling a PHP script on an external site.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    file_get_contents relies on settings in php.ini.

    if you are calling PHPs CLI direct from cron, then IIRC CLI has its own php.ini file, which may well have different settings from php.ini.

    Look for both : safe_mode, and allow_url_fopen().

    If you cannot or don't want to unset these security settings then have your script call a cURL enabled script.

    Or, you could leave your script in a public folder and call it direct with CRON and pipe the output to dev/null.

    www.yoursite .com/dostuff.php
    [a script which contains your file_get_contents() call along with other stuff]


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •