HOME > PHP

プログラムからデータ送信

  curl利用する方が望ましい

curl利用する

$kintai = ‘http://nsme.red/test/reception.php’;     
$data =array(
            ‘stafid’ =>’1234′,
            ‘date’=>’2018/10/14’,
            ‘start’=>’08:30’,
            ‘end’=>’17:30’,
            ‘rest’=>’60’
            );
        //cURLセッションを初期化する
$ch = curl_init();
//URLとオプションを指定する
curl_setopt($ch, CURLOPT_URL, $kintai);
curl_setopt($ch, CURLOPT_POST, true); //POSTが有効
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 
//URLの情報を取得する
$res =  curl_exec($ch);
 
//結果を表示する
var_dump($res);
 
//セッションを終了する
curl_close($ch);

file_get_contentsを利用

$kintai = ‘http://nsme.red/test/reception.php’;     
$data =array(
            ‘stafid’ =>’1234′,
            ‘date’=>’2018/10/14’,
            ‘start’=>’08:30’,
            ‘end’=>’17:30’,
            ‘rest’=>’60’
            );
        
        $data = http_build_query($data, “”, “&”);
        $options =array(
        ‘http’ => array(
                ‘method’=> ‘POST’,
                ‘header’=> ‘Content-Type: application/x-www-form-urlencoded’,
                ‘content’ => $data
        )
        );
        // ストリームコンテキストの作成
        $context = stream_context_create($options);
        // POST送信
        $contents = file_get_contents(‘http://core.nsme.net/test/reception.php’, false, $context);
        // reception.php のレスポンスを表示
        echo $contents;