简体   繁体   English

get_meta_tags()连接被拒绝

[英]get_meta_tags() connection refused

In my php code i am using get_meta_tags() to fetch meta information from the site. 在我的PHP代码中,我使用get_meta_tags()从网站获取元信息。 But my proxy server is refusing connections and i am getting the following error: 但我的代理服务器拒绝连接,我收到以下错误:

Warning: get_meta_tags(http://www.espncricinfo.com/) [function.get-meta-tags]: failed to open stream: No connection could be made because the target machine actively refused it 警告:get_meta_tags(http://www.espncricinfo.com/)[function.get-meta-tags]:无法打开流:无法建立连接,因为目标计算机主动拒绝它

Can anyone tell me how to by pass the proxy in my php code? 任何人都可以告诉我如何在我的PHP代码中传递代理?

I tried setting proxy in Eclipse XDebug configuration but i don't think that is the right way to do it. 我尝试在Eclipse XDebug配置中设置代理,但我不认为这是正确的方法。

In curl i specified the proxy as curl_setopt($ch, CURLOPT_PROXY, "host:port"); curl我将代理指定为curl_setopt($ch, CURLOPT_PROXY, "host:port"); which worked fine but in php i don't know the procedure. 哪个工作正常,但在PHP我不知道程序。 Any help would be appreciated. 任何帮助,将不胜感激。

-Adithya -Adithya

By default PHP doesn't use a proxy. 默认情况下,PHP不使用代理。 To bypass via a proxy, you can add a proxy for all functions using the http stream wrapper Docs (that wrapper is taking care for "filenames" starting with http:// or https:// ) like in your get_meta_tags Docs function example. 要绕过代理,您可以使用http流包装器Docs (包装器正在处理以http://https://开头的“文件名”)为所有函数添加代理,就像在get_meta_tags Docs函数示例中一样。

There are many HTTP context options Docs , the one you're looking for is proxy . 有许多HTTP上下文选项文档 ,您正在寻找的是proxy

As get_meta_tags does not accept a context parameter (only a filename parameter), you need to change the so called default context which is (normally) used by PHP functions accepting a filename parameter. 由于get_meta_tags不接受上下文参数(仅限文件名参数),因此需要更改所谓的默认上下文 ,该上下文通常由接受文件名参数的PHP函数使用。 It's set with stream_context_get_default Docs . 它是使用stream_context_get_default 文档设置的。

$opts = array(
    'http' => array(
        'proxy' => 'tcp://127.0.0.1:8000'
    )
);
stream_context_get_default($opts);

Unfortunately get_meta_tags looks like an exception to the general rule to use stream wrappers at all (at least with my PHP 5.3.8 version). 不幸的是, get_meta_tags看起来像使用流包装器的一般规则的例外(至少在我的PHP 5.3.8版本中)。 But no worries, you can slip the data you would like to obtain the meta tags from into get_meta_tags with using the default context. 但不用担心,您可以使用默认上下文将您想要获取元标记的数据放入get_meta_tags

This can be done with the data:// stream wrapper Docs . 这可以使用data:// stream wrapper Docs完成 A little helper function takes care for the conversion: 一个小辅助函数负责转换:

/**
 * obtain $filename content as data:// URI
 * 
 * @link http://php.net/manual/en/wrappers.data.php
 *
 * @param string $filename
 * @return string data:// URI
 */
function filename_data_uri($filename)
{
    $buffer = file_get_contents($filename);

    $mime = 'text/plain';
    # obtain mime type and charset from http response (if available)
    if (isset($http_response_header))
        foreach($http_response_header as $header)
            sscanf($header, 'Content-Type: %[^]]', $mime)
    ;

    return "data://$mime;base64,".base64_encode($buffer);       
};

This function allows to obtain contents from an URL with file_get_contents which makes use of the default stream context. 此函数允许使用file_get_contents从URL获取内容,该文件使用默认流上下文。 That is the one the proxy was configured for. 这是代理配置的那个。

You then can combine this with get_meta_tags : 然后,您可以将其与get_meta_tags结合使用:

$url = 'http://www.espncricinfo.com/';
$url = filename_data_uri($url);
$meta_tags = get_meta_tags($url);

get_meta_tags now operates on the contents of $url which has been already fetched with the filename_data_uri function while using the proxy. get_meta_tags现在对$url的内容进行操作,该内容在使用代理时已经使用filename_data_uri函数获取。 The full example: 完整的例子:

$url = 'http://www.espncricinfo.com/';
$proxy = 'tcp://host:port';

// configure default context to use proxy
$opts['http']['proxy'] = $proxy;
$resource = stream_context_get_default($opts);

// obtain url contents with default context
$data = filename_data_uri($url);
$meta_tags = get_meta_tags($data);
print_r($meta_tags);

/**
 * obtain $filename content as data:// URI
 * 
 * @link http://php.net/manual/en/wrappers.data.php
 *
 * @param string $filename
 * @return string data:// URI
 */
function filename_data_uri($filename)
{
    $buffer = file_get_contents($filename);

    $mime = 'text/plain';
    # obtain mime type and charset from http response (if available)
    if (isset($http_response_header))
        foreach($http_response_header as $header)
            sscanf($header, 'Content-Type: %[^]]', $mime)
    ;

    return "data://$mime;base64,".base64_encode($buffer);       
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM