MacOSX 10.5環境でPHPのfsockopen等がうまく動かない件

追加して調べています。
現時点の結論だけ書くと、Apache経由の名前解決がドボン
(追記)だったんだけど、Macを再起動したら動くようになった。なんだそれ。

名前解決の調査

nslookupを使って名前解決を調べたのですが、問題なし。
/etc/resolv.confも適切。

PHP経由の名前解決をチェック

PHPの名前解決の挙動がコマンド実行時とブラウザからのアクセス時で異なる件について - oranieの日記
と現象自体は同じようだ。

テストプログラムを作ってみた。

<?php
$text = file_get_contents('http://google.jp/robots.txt');
print_r($text);
exit();
?>

CLI版のPHPで起動してみるとちゃんと動く。
>|sh|
mac-mini:htdocs $php test.php
User-agent: *
...(略)...
mac-mini:htdocs $

apache経由でhttp://localhost/test.phpにアクセスするとエラーになる。

Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/local/apache2/htdocs/test.php on line 2

Warning: file_get_contents(http://google.jp/robots.txt) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/local/apache2/htdocs/test.php on line 2

perl経由の名前解決をチェック

LWP::Simpleを使ってチェックしようと思ったらLWP::Simpleが入ってねえし。
CPANを使って入れてみる。CAPN自体もこの環境では初めて起動した。

mac-mini:~ $ sudo -H cpan
...()...
Would you like me to configure as much as possible automatically? [yes]

ここでリターンを押すと自動的に設定される。終わるとプロンプトが出るので、LWP::Simpleの設定を確認したら入ってない。

cpan[1]> i LWP::Simple
...(初回起動でなんか出た)...
Going to write /var/root/.cpan/Metadata
Module id = LWP::Simple
    DESCRIPTION  Simple procedural interface to libwww-perl
    CPAN_USERID  LWWWP (The libwww-perl mailing list <libwww@perl.org>)
    CPAN_VERSION 5.827
    CPAN_FILE    G/GA/GAAS/libwww-perl-5.833.tar.gz
    DSLIP_STATUS Rmpf? (released,mailing-list,perl,functions,)
    INST_FILE    (not installed)

とりあえずインストール。途中依存パッケージ入れるか聞かれるのでリターンで決定した。

cpan[2]> install LWP::Simple
...()...
cpan[2]> i LWP::Simple
Module id = LWP::Simple
    DESCRIPTION  Simple procedural interface to libwww-perl
    CPAN_USERID  LWWWP (The libwww-perl mailing list <libwww@perl.org>)
    CPAN_VERSION 5.827
    CPAN_FILE    G/GA/GAAS/libwww-perl-5.833.tar.gz
    DSLIP_STATUS Rmpf? (released,mailing-list,perl,functions,)
    MANPAGE      LWP::Simple - simple procedural interface to LWP
    INST_FILE    /opt/local/lib/perl5/site_perl/5.8.9/LWP/Simple.pm
    INST_VERSION 5.827


cpan[3]> quit
Terminal does not support GetHistory.
Lockfile removed
mac-mini:~ $

ようやく本題に入れる。テストプログラムを作ってみた。

#!/opt/local/bin/perl

use strict;
use warnings;
use LWP::Simple;
my $text = get('http://google.jp/robots.txt');
print $text;
exit;

コマンドから起動してみるとちゃんと動く。

mac-mini:htdocs ./test.cgi
User-agent: *
...()...
mac-mini:htdocs $

apache経由でhttp://localhost/test.cgiにアクセスすると500エラーになった。
調べてみたら、LWPは名前解決出来なかった場合は500をねつ造するらしいので同じ症状だな。

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

再起動したら動いた!

apacheを再起動しても駄目だったけど、システムを再起動したら名前解決が動いた。
なんですと!!

portでアップデートした後は再起動しないと挙動がおかしい、、、とφ(-_-)