SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with file_get_contents

    I'm new to php so i guess the answer to this problem is simple but after a few hours i'm non the wiser. I keep getting an error

    Warning: file_get_contents($url1) [function.file-get-contents]: failed to open stream: No such file or directory in C:\wamp\www\php_sandbox\rework.php on line 41

    i decided to add a bit of error checking and echo out url1 which echos out the correct address and it works if I copy and paste it into my browser..

    I've used the file_get_contents function earlier in my code with no problems, although i did specify the address rather than give it a string..

    can anyone see any problems with this?


    $url1 = "http://www.widgets.co.uk".$urlsite;
    $data1 = file_get_contents('$url1');
    if(!$data1){
    echo "$url1";
    die("File get contents is broke");
    }
    else{
    echo "Success";
    }

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    php does not expand variables in single quoted strings. You can use double quotes, but no quotes are needed here at all.
    PHP Code:
    $data1 file_get_contents($url1); 

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply, I've made that change but i'm still getting the same error msg. The error msg now shows the full URL which if i copy and paste into my browser work fine.. Any ideas on why it cant get it? here is the full error msg.

    Warning: file_get_contents(http://www.reed.co.uk/job-details/Ipswich/Accounts-Admin/Accounts-Administrator/?JobID=17835906&&pg=3&s=174&sr=1&FromSector=1&lit=3" id="ctl00_Main_rptJobSearchResults_ctl01_lnkJobtitle) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\wamp\www\php_sandbox\rework.php on line 41

    I've used file_get_contents earlier in the script would this have anything to do with it?

    - Ive retried the script by giving it the url directly and it works, could there be some hidden char or something in the string making it screw up?
    Last edited by Darren Tidmarsh; Apr 26, 2009 at 23:22.

  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)
    I don't think its going to like this bit of the url 3" id=".

    You could try running urlencode() over the url first.

    Always try it out on a simpler url from their site, this will confirm that you have correct permissions to go out onto the web and bring in data. ( safe_mode, allow_url_fopen etc in your ini file, local settings in a firewall etc).

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it looks like it is something with the URL but i'm not sure on the best fix for it. I rewrote my code for testing which is below

    Code:
    <?php
    
    
    // main page to get link from
    $data = file_get_contents('http://www.reed.co.uk/job/searchresults.aspx?s=174&sr=1&FromSector=1&pg=3');
    
    // Get first
    $location1 = strpos($data,"<h4><a href=\"");
    $location1 = ($location1 + 13);
    $location2 = strpos($data,"\">", $location1);
    $next = $location2;
    $location2 = ($location2 - $location1); 
    $urlsite = substr($data,$location1,$location2);
    
    echo "<b>Extracted Link :</b>".$urlsite."<br /><br />";
    
    
    $url = "http://www.reed.co.uk";
    //$url .= urlencode($urlsite);
    $url .= $urlsite;
    echo "<b>Combined Link :</b>".$url."";
    echo "<br /><br />";
    
    echo "Test 1:";
    $data = file_get_contents('http://www.reed.co.uk/job-details/Spalding/Accounts-Admin/ledger-clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2"%20id="ctl00_Main_rptJobSearchResults_ctl00_lnkJobtitle');
    	if(!$data){
    		//die("File get contents is broke");
    	}else{
    	Echo "data  get success<br /><br />";
    		}
    
    echo "Test 2:";
    $data1 = file_get_contents($url);
    	if(!$data1){
    		//die("File get contents is broke");
    	}else{
    	Echo "data1  get success";
    		}
    
    ?>
    Here is the oputput

    Code:
    Extracted Link :/job-details/Spalding/Accounts-Admin/ledger-clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2" id="ctl00_Main_rptJobSearchResults_ctl00_lnkJobtitle
    
    Combined Link :http://www.reed.co.uk/job-details/Spalding/Accounts-Admin/ledger-clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2" id="ctl00_Main_rptJobSearchResults_ctl00_lnkJobtitle
    
    Test 1:data get success
    
    Test 2:
    Warning: file_get_contents(http://www.reed.co.uk/job-details/Spalding/Accounts-Admin/ledger-clerk/?JobID=17817202&amp;&amp;pg=3&amp;s=174&amp;sr=1&amp;FromSector=1&amp;lit=2" id="ctl00_Main_rptJobSearchResults_ctl00_lnkJobtitle) [function.file-get-contents]: failed to open stream: HTTP request failed! in C:\wamp\www\php_sandbox\rework1.php on line 33
    The only differance I can see in the two links is a %20 I tried urlencode but cant seem to make that work. anyway I posted the full code so if anyone is interested they can try it themself.. The only other way I can think of is inserting the %20 into the string myself..

    Thanks again for them help

  6. #6
    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)
    Something went wrong when you extracted the link, its brought some of the dom along with it.

    this is a correct link:

    http://www.reed.co.uk/job-details/Sp...-Admin/ledger-
    clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2

    this is what you extracted:

    Extracted Link :/job-details/Spalding/Accounts-Admin/ledger-
    clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2"
    id="ctl00_Main_rptJobSearchResults_ctl00_lnkJobtitle

    You should have stopped at the first ".

    If you cannot get the parse right, then you could explode() on " and use the first segment [0]

    $url = explode("\"", $extracted_link) ;

    echo $url[0] ;

    nasty, but should work at a pinch - you are best finding the error in whatever regex you are using, why not post that with some typical test data?

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cups! I love you

    I looked in the source for the file i was trying to get the link from and thats where I got the long link, I did'nt know stopping at the first quote mark would give the same result..

    I did make a work around with string replace but its ugly and i don't need it now.

    I'll get back to it, thanks for your time great help!

  8. #8
    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)
    Great, I love you too, though I am happy you didn't say it like this. :ROFL:

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    May I also point in the way of DOM? It's a bit like learning the way of the Jedi, to learn it you need to befriend a 'little-wierdo', but once you do master it - you can do some pretty funky stuff quite easily.

    PHP Code:
    <?php
    $oXPath 
    = new DOMXPath(@DOMDocument::loadHTMLFile('http://www.reed.co.uk/job/searchresults.aspx?s=174&sr=1&FromSector=1&pg=3'));
    foreach(
    $oXPath->query("//div[contains(@class, 'resultEntry')]/h4/a") as $oNode)
    {
        
    printf(
            
    'http://www.reed.co.uk%s',
            
    $oNode->getAttribute('href')
        );
    }
    /*
        http://www.reed.co.uk/job-details/Spalding/Accounts-Admin/ledger-clerk/?JobID=17817202&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Near-Chester/Accounts-Admin/Programme-Administrator-Near-Chester/?JobID=17817136&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Ipswich/Accounts-Admin/Finance-Administrator/?JobID=17851002&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/London/Accounts-Admin/IBA-Insurance-Accounts-Assistant/?JobID=17828207&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Aldershot/Accounts-Admin/Accounts-Payble-Clerks/?JobID=17816818&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Bromley/Accounts-Admin/RECONCILIATIONS-Clerk/?JobID=17850808&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Newhaven/Accounts-Admin/Purchase-Ledger-Administrator/?JobID=17572026&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/London/Accounts-Admin/SAP-Master-Data-Analyst/?JobID=17841513&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Stratford-Upon-Avon/Accounts-Admin/Accounts-Administrator/?JobID=17816463&&pg=3&s=174&sr=1&FromSector=1&lit=2
        http://www.reed.co.uk/job-details/Leatherhead/Accounts-Admin/excel-administrator/?JobID=17841422&&pg=3&s=174&sr=1&FromSector=1&lit=3
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2004
    Location
    UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    silver thanks for that, gonna save it and take a look at it when i get a better understanding of the language.. it would just make my head hurt at the moment..

    Thanks guys, been a great help.


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
  •