Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php): failed to open stream: No such file or directory in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24

Warning: include(): Failed opening '/home/blog66rr/public_html/wp-content/plugins/hyper-cache/cache.php' for inclusion (include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/blog66rr/public_html/wp-content/advanced-cache.php on line 24
IP – unethost無限空間虛擬主機 技術分享部落格

php指定對外連線使用的IP位置

一般而言,主機商的主機,不會只有一個IP位置。通常會有超過一個,以備不時之需。然而,由於網路上的有些服務,常會鎖IP,所以透過php去存取遠端的網站時,有時候就必須考慮這個對外連線使用的IP位置。

使用ifconfig指令,通常eth0上附加的這個ip就是預設的對外連線用IP(outgoing IP address)。由於使用者登入主機時,是一般user,無法使用ifconfig。所以要用其它方法來判斷。其中一種方式是curl。下面這個指令就可以得到系統預設對外主動連線時所使用的IP了。

curl -s ip.appspot.com

而,如果對外是透過php去存取遠端的資料,又必須指定php一定要透過某個網路介面/IP address來連線的話,可以考慮使用php curl library。範例的code如下:

<?
$url = “http://www.google.com”
$curlh = curl_init($url);
curl_setopt($curlh, CURLOPT_USERAGENT, $uagent);
curl_setopt($curlh, CURLOPT_INTERFACE, “888.888.888.888“);
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curlh);

echo $result;
?>

其中,”888.888.888.888″,就是要被指定用對外連線用的IP位置。