A simple way to communicate a server via HTTP from PHP script. GET only.
色々なやり方の紹介をみたが、できるだけ簡単に書きたかったので自分も書いてみる。でも、GET だけ。
// $url : request URL // $qs : an array contains QUERY STRING function my_http_get($url) { $h=fopen($url,'r'); $meta_data=stream_get_meta_data($h); // both PHP4, PHP5 OK $headers=$meta_data['wrapper_data']; $result['content']=stream_get_contents($h); // only PHP5, you need write the function for PHP4 fclose($h) if (preg_match('/^HTTP\/([0-9]\.[0-9]) ([0-9]+)/',$headers[0],$matches)) { $result['response']=$matches[2]; } return $result; }
In addition to the above, the following would be useful to make QUERRY STRING from an array.
おまけ。QUERRY STRINGを配列から作る処理。
if (is_array($qs)) { $qs_encoded='?'; foreach($qs as $key=>$value) { if (substr($qs_encoded,-1)!='?') { $qs_encoded.='&'; } $qs_encoded.=$key.'='.rawurlencode($value); } } else { $qs_encoded=$qs; } }