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.

[file_get_contents]: failed to open stream "need help"

Discussion in 'PHP' started by DaniW, Aug 16, 2006.

  1. #1
    Hello,
    I've got the following problem:
    I want to include the googleSearch into my wiki.
    Therefore I use the following implementation for mediawiki:
    
    <?php
    
    # Created by Matt Hart, Intuit, Inc. http://meta.wikimedia.org/wiki/User:MHart
    # Note that this is not really an extension in the scrict sense - it isn't added
    # to the extensions list and such. Rather it replaces the search.
    
    function wfGoogleSearch( $par='' ) {
    
    global $wgRequest, $wgUser;
    global $wgOut, $wgSitename;
    global $wgScriptPath;
    
    $searchPage = new SpecialSearch( $wgRequest, $wgUser );
    
    $search = $wgRequest->getText( 'search', $par );
    $start = $wgRequest->getText( 'start', $par);
    
    $wgOut->setPageTitle($search . " - Google Search");
    
    $sitesearch = urlencode($_SERVER["HTTP_HOST"] . $wgScriptPath);
    
    # Google URL Parameters
    # site=ext_name  : this is the Google appliance (GA) "collection" to search
    # client=name    : this is the GA client that is allowed to make requests
    # as_sitesearch= : this restricts the results to this specific wiki, even on shared domains
    #                  so wikis.intuit.com/first  gets its own results
    #                        likewise wikis.intuit.com/second gets its own
    $url = 'http://googlesearchurl.yoursite.com/search?q=' . urlencode($search) . 
    '&output=xml&site=
    ext_name&client=name&as_sitesearch=' . $sitesearch . '&start=' . $start;
    
    $xmldata = file_get_contents($url);
    
    # This saves the file to a temp location so that XSLTPROC can
    # run on it. If you have Sablotron installed with PHP 5, you can
    # use it's XSLT functions. The following method, however, works with
    # any version of PHP.
    
    $xmlfile = tempnam("","");
    $file = fopen($xmlfile,"w");
    fwrite($file,$xmldata);
    fclose($file);
    
    $results = xsltproc('extensions/Google.xslt',$xmlfile);
    
    $wgOut->addHTML($results);
    
    unlink($xmlfile);
    
    $wgOut->output();
    return;
    }
    
    function xsltproc($xslfile, $xmlfile)
    {
        $toexec = "xsltproc " . $xslfile . " " . $xmlfile;
        exec($toexec,$results);
        return implode("",$results);
    }
    ?>
    
    
    Code (markup):
    For the url: googlesearch.yoursite.com I use my local directory, where I am testing my extension: http://localhost/wiki/index.php/

    If i am now testing this extension, searching for a word, there will appear the following fault:[file_get_contents]: failed to open stream: HTTP request failed!

    Can you help me?
    I'm disappointed that i cannot correct the mistake by my own..

    Best regards,
    Daniel

    ------------------
    XAMPP 1.5.3a
    PHP 5.1.4
    Apache 2.2.2
    Mysql 5.0.21
     
    DaniW, Aug 16, 2006 IP
  2. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try putting this right before the line that calls file_get_contents.
    ini_set ('allow_url_fopen', 1);
     
    exam, Aug 17, 2006 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try something in the line of
    
    ob_start();
    $xmldata = ob_get_contents($url);
     ob_end_clean();
    
    Code (markup):
     
    123GoToAndPlay, Aug 20, 2006 IP
  4. petronel

    petronel Peon

    Messages:
    113
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    could be from this one:
    $url = 'http://googlesearchurl.yoursite.com/....
    Code (markup):
     
    petronel, Aug 20, 2006 IP
  5. dcole07

    dcole07 Peon

    Messages:
    135
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $xmldata = file_get_contents($url);
    Code (markup):
    make sure that $url doesn't end in a space (or tab, or newline... ), I was helping a guy who couldn't get file_get_contents() just to day and he took a list of URL and exploded them but forgot to use rtrim() to remove extra stuff and it gave him errors.

    ---- ---
    also, is the URL a real website?
     
    dcole07, Aug 20, 2006 IP
  6. DaniW

    DaniW Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That works,
    but now there follows another error:

    Call to a member function getID() on a non-object in..
    (this line)
    if ( $wgOut->isArticle() and (!isset( $oldid ) or isset( $diff )) and 0 != $wgArticle->getID() )
    Code (markup):
    and this line I think refers to another file where I changed the normal SearchEngine:
    if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
         // Commented OUT the default search
         // require_once( 'includes/SpecialSearch.php' );
         // $title = Title::makeTitle( NS_SPECIAL, 'Search' );         
         // wfSpecialSearch();
         // Add GoogleSearch here AND see the line below where it's added
         wfGoogleSearch();} 
    else if( !$title or $title->getDBkey() == '' ) {
         $title = Title::makeTitle( NS_SPECIAL, 'Badtitle' );
         # Die now before we mess up $wgArticle and the skin stops working
         throw new ErrorPageError( 'badtitle', 'badtitletext' );
    } else if ( $title->getText() == 'Search') {
         wfGoogleSearch();
    } else if ( $title->getInterwiki() != '' )...
    Code (markup):

    Any ideas why this error occurs?Is the index of the "Search"Array zero and generates this one?
     
    DaniW, Aug 21, 2006 IP
  7. DaniW

    DaniW Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Oh, I haven't seen a fault in the description of this extension, so this topic is closed because it's no longer interesting for me. Thanks for your help!
     
    DaniW, Aug 21, 2006 IP