我可以通过特定的IP(如果计算机具有两个IP)在PHP中打开套接字吗? [英] Can I open socket in PHP from a specific IP (if the machine has two IPs)?

查看:85
本文介绍了我可以通过特定的IP(如果计算机具有两个IP)在PHP中打开套接字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPMailer,它使用fsockopen访问SMTP服务器.

I'm using PHPMailer and it uses fsockopen to access the SMTP server.

但是机器有两个IP,它们具有不同的反向DNS记录.因此,在电子邮件标题中,我得到了以下信息:

But the machine has two IPs with different reverse DNS records. So in email headers I got the following:

Received: from one-server.tld (HELO another-server.tld) ...

我需要隐藏one-server.tld以便支持another-server.tld.但是我需要两个IP及其当前的RDNS设置.

I need to hide one-server.tld in favor of another-server.tld. But I need both IPs with their current RDNS settings.

推荐答案

我认为使用fsockopen是不可能的.但是它可能在curlfopenstream函数中使用.您需要的是 stream_socket_client()函数.

I think its not possible using fsockopen. But its possible in curl, fopen and stream functions. What you need is stream_socket_client() function.

有一些方法可以实现它.

Here are some ways to achieve it.

  1. 使用可以在fopen函数族和stream函数族中使用的上下文参数.参见示例.

  1. Using context parameters which can be used in fopen function family and stream function family. See the example.

$opts = array(
    'socket' => array(
        'bindto' => '192.168.0.100:0',
    ),
);
// create the context...
$context = stream_context_create($opts);
$contents = fopen('http://www.example.com', 'r', false, $context);

stream_socket_client

$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $opts);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}

  • 使用 socket_bind . PHP.NET在此处.

  • Using socket_bind. PHP.NET got a simple example here.

    这篇关于我可以通过特定的IP(如果计算机具有两个IP)在PHP中打开套接字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆