【问题标题】:How can i use fsockopen on my site?如何在我的网站上使用 fsockopen?
【发布时间】:2012-10-06 18:41:30
【问题描述】:

我在我的网站上使用货币代码。带有以下内容:

<?php
$contents = file_get_contents('http://www.tcmb.gov.tr/kurlar/today.html'); 
$contents = iconv("windows-1254" , "utf8" , $contents);

$dollar = preg_match('~ABD DOLARI\s+(.+?)\s+(.+?)\s+~i', $contents, $matches) ? array('buying' => $matches[1], 'selling' => $matches[2]) : ''; 
$euro = preg_match('~EURO\s+(.+?)\s+(.+?)\s+~i', $contents, $matches) ? array('buying' => $matches[1], 'selling' => $matches[2]) : ''; 
$gbp = preg_match('~İNGİLİZ STERLİNİ\s+(.+?)\s+(.+?)\s+~i', $contents, $matches) ? array('buying' => $matches[1], 'selling' => $matches[2]) : ''; 
$chf = preg_match('~İSVİÇRE FRANGI\s+(.+?)\s+(.+?)\s+~i', $contents, $matches) ? array('buying' => $matches[1], 'selling' => $matches[2]) : ''; 

echo ' 
    <table class="form" style="background:#fff;width:300px;margin-left:14px;"> 
        <tr style="border-bottom:1px solid #e4e4e4;">
        ..

但是今天我的网站报错了:

Warning: eval() (/var/www/vhosts/mysite.com/httpdocs/modules/php/php.module(80) : eval()'d code dosyasının 2 satırı) içinde file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0.

我确实向我的托管支持询问了这个问题,他们说:

“不要使用 fopen 选项,请使用 'fsockopen'” 但我不知道该怎么做?

请帮帮我。谢谢。

【问题讨论】:

  • 不知道哪里可以改代码?
  • 使用fsockopen 不是一个好的解决方案。改用 curl。您可能还想告诉您的托管公司 url_fopen_wrappers 是完全安全的,只要 url 包含被禁用。
  • 偏执狂的安全性再次来袭!

标签: php eval fopen fsockopen


【解决方案1】:

然后改用curl。从远程服务器替换file_get_contents 的函数是:

function get_web_page( $url ) {
    $options = array(
    CURLOPT_RETURNTRANSFER => true,     // return web page
    CURLOPT_HEADER         => false,    // don't return headers
    CURLOPT_FOLLOWLOCATION => true,     // follow redirects
    CURLOPT_ENCODING       => "",       // handle all encodings
    CURLOPT_USERAGENT      => "spider", // who am i
    CURLOPT_AUTOREFERER    => true,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
    CURLOPT_TIMEOUT        => 120,      // timeout on response
    CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
        //If you want error information, 'return $header;' instead.
    return $content;
}

从那里将$contents = file_get_contents('http://www.tcmb.gov.tr/kurlar/today.html'); 更改为$contents = get_web_page('http://www.tcmb.gov.tr/kurlar/today.html');

【讨论】:

    猜你喜欢
    • 2017-10-16
    • 2013-03-22
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2015-04-11
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多