有没有办法让php脚本使用特定的ip地址执行

有没有办法让php脚本使用特定的ip地址执行,php,Php,有没有办法让php脚本使用file_get_内容来利用不同于服务器ip地址的ip地址?我们有5个ip地址,希望为此使用一个特定的ip地址。是的,这是可能的。但是,您必须配置要使用的流上下文 <?php // context options $ctxopts = array( 'socket' => array( 'bindto' => '192.168.0.100:0', ), ); // create the context... $cont

有没有办法让php脚本使用file_get_内容来利用不同于服务器ip地址的ip地址?我们有5个ip地址,希望为此使用一个特定的ip地址。

是的,这是可能的。但是,您必须配置要使用的流上下文

<?php
// context options
$ctxopts = array(
    'socket' => array(
        'bindto' => '192.168.0.100:0',
    ),
);

// create the context...
$context = stream_context_create($ctxopts);

// ...and use it to fetch the data
echo file_get_contents('http://www.example.com', false, $context);
在php.net上易于访问的

context
A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by NULL.
其余的解释

$opts = array(
    'socket' => array(
        'bindto' => '123.123.123.123:0', // 0 for automatically determine port
    )
);
最终代码:

$opts = array(
    'socket' => array(
        'bindto' => '123.123.123.123:0', // 0 for automatically determine port
    )
);    
$stream = stream_context_create($ctxopts);
echo file_get_contents('http://www.website', false, $stream);

@skrilled我相信OP想要更改
文件获取内容的原始IP请求。从拥有您想要的IP的机器执行调用…哦,我现在知道了。我的缺点是看东西太快。不过,文档中仍然回答了这个问题。