http proxy support

2009-10-20
2013-05-28
  • Alexander Pohl
    Alexander Pohl
    2009-10-20

    Hello,

    It would be nice if refbase would support http proxies. If I try to import from pubmed or crossref I get the following error message:


    Warning: fopen(http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=text&rettype=medline&tool=refbase&email=info%40refbase.net&id=12748199) : failed to open stream: Connection refused in /home/alex/public_html/refbase/includes/include.inc.php on line 5213

    Warning: Cannot modify header information - headers already sent by (output started at /home/alex/public_html/refbase/includes/include.inc.php:5213) in /home/alex/public_html/refbase/import_modify.php on line 557


    I guess it has to do with the call of the php fopen function, which doesn't support proxies directly. I have written a little curl script, which supports proxies, but I don't know where to put this code into refbase (I am also not a programmer, even this little code snippet took me a while to get it to work).

        <?php
        // create file handle
        $fp = fopen('/tmp/rss.xml', 'w');
       
        // create a new cURL resource
        $ch = curl_init();
       
        // set URL and other appropriate options
        $options = array(CURLOPT_URL => 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=text&rettype=medline&tool=refbase&email=info%40refbase.net&id=12748199',
                         CURLOPT_HEADER => true,
                         CURLOPT_RETURNTRANSFER => true,
                         CURLOPT_HTTPPROXYTUNNEL => false,
                         CURLOPT_PROXY => 'wwwcache.example.com',
                         CURLOPT_PROXYPORT => '8080',
                         // syntax for proxy credentials is ':'
                         CURLOPT_PROXYUSERPWD => '',
                         CURLOPT_FILE => $fp);
       
        curl_setopt_array($ch, $options);
       
        // grab URL and pass it to the browser
        curl_exec($ch);
       
        // close cURL resource, and free up system resources
        curl_close($ch);
       
        // close file handle
        fclose($fp);
        ?>

     
  • Alexander Pohl
    Alexander Pohl
    2009-10-21

    Hi,

    here is a first attempt to implement proxy support into refbase. It works at my site. Note: the changes to the regex expression in import.inc.php needed for crossref to work are not in the bleeding edge svn repository!
    See post

    ToDo:

         - move the proxy parameters into ini.inc.php
         - decide whether to use proxy or not from ini.inc.php
         - error handling

    Replacement for function fetchDataFromURL() in include.inc.php:

        function fetchDataFromURL($sourceURL)
         {
                    // specifiy if proxy is needed
                    $use_proxy = true;

                    // give details of http proxy server here
                    $http_proxy = "wwwcache.example.com";
                    $http_proxy_port = "8080";

                    // syntax for proxy credentials is ":"
                    $http_proxy_userpwd = "";

                    // create a new cURL resource
                    $ch = curl_init();

                    // set URL and other appropriate options
                    if ($use_proxy)
                    {
                            $options = array(CURLOPT_URL => $sourceURL,
                                             CURLOPT_HEADER => false,
                                             CURLOPT_RETURNTRANSFER => true,
                                             CURLOPT_HTTPPROXYTUNNEL => false,
                                             CURLOPT_PROXY => $http_proxy,
                                             CURLOPT_PROXYPORT => $http_proxy_port,
                                             CURLOPT_PROXYUSERPWD => $http_proxy_userpwd,
                                            );
                    }
                    else
                    {
                            $options = array(CURLOPT_URL => $sourceURL,
                                             CURLOPT_HEADER => false,
                                             CURLOPT_RETURNTRANSFER => true,
                                            );
                    }

                    curl_setopt_array($ch, $options);

                    // grab URL and pass it to the browser
                    $data = curl_exec($ch);

                    // close cURL resource, and free up system resources
                    curl_close($ch);

                    return $data;
            }

      : https://sourceforge.net/projects/refbase/forums/forum/218757/topic/3401506