有没有办法让php脚本使用file_get_contents来使用不同于服务器IP地址的ip地址?我们有5个IP地址,并希望为此目的利用特定的地址。
答案 0 :(得分:1)
是的,这是可能的。您必须配置要使用的流上下文。
<?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);
上获得更多信息
答案 1 :(得分:0)
根据php.net上可以轻松访问的documentation
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.
stream_context_create()'s documentation解释其余的
$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);