1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting error while calling [function.file-get-contents]: failed to open stream

Discussion in 'PHP' started by desertofwater, Jul 1, 2012.

  1. #1
    Hi,
    I have used the following code
    SEMrush

    <?php
    	$db	= mysql_connect("localhost", "db_user", "hotnwild");
    	mysql_select_db("db_temp", $db);
    	$id = mysql_query("SELECT * FROM table",$db);
    	while ($check = mysql_fetch_assoc($id)){
    		$new_id=$check['id'];
    		$result = file_get_contents('http://graph.facebook.com/'.$new_id);
    		if ($result == 'false') mysql_query("delete from table WHERE id = '$new_id'",$db);
    	}
    ?>
    Code (markup):
    while using this code facing an error

    "Warning: file_get_contents(http://graph.facebook.com/240163152769863) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /home/dbstore/public_html/check.php on line 8"
     
    desertofwater, Jul 1, 2012 IP
    SEMrush
  2. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #2
    I am guessing you are trying to check to get users details using the facebook userid/name.
    But if you are wanting to delete the user if the request fails and doesn't exist then you can use @file_get_contents instead which will suppress any errors that it comes across.


    However if you should be getting a result and you are for some reason being forbidden access, then this could be another problem, possibly down to your hosts settings.
    I will try your code on my hosting and post my results in a few moments.
     
    geforce, Jul 1, 2012 IP
  3. geforce

    geforce Active Member

    Messages:
    173
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #3
    After trying to code on my hosting it works perfectly fine.
    So I can only thing there is some configuration on your hosting, stopping you from accessing that url.
    Have you tried cURL instead of file_get_contents?
    Here is the code I used (for reference):

    
    <?php    
    //$db    = mysql_connect("localhost", "db_user", "hotnwild");    
    //mysql_select_db("db_temp", $db);    
    //$id = mysql_query("SELECT * FROM table",$db);    
    //while ($check = mysql_fetch_assoc($id))
    {        
    $new_id='240163152769863';        
    $result = file_get_contents('http://graph.facebook.com/'.$new_id);        
    if ($result == 'false') 
    //mysql_query("delete from table WHERE id = '$new_id'",$db);        
    {            
    echo "false";        
    }        
    else        
    {           
    echo "true";        
    }    
    //}
    ?>
    PHP:
    here is a demo: http://tutorial-resource.com/testing/fbgraph.php
     
    geforce, Jul 1, 2012 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,465
    Likes Received:
    539
    Best Answers:
    21
    Trophy Points:
    245
    #4
    A lot of web hosts will disable the use of file_get_contents() for remote files as it can be a security risk.

    Use cURL. If you can't use cURL then use a SOCKET function.
     
    NetStar, Jul 1, 2012 IP