더 나은 프로그래머가 되자

php파일에 post값 전달하기,file_get_contents 본문

언어/PHP

php파일에 post값 전달하기,file_get_contents

greathuman 2015. 2. 11. 13:45

php에서 file_get_contents 함수를 이용해서 다른 파일을 불러올때가 있다 (특히 이메일 발송시 메일 폼 불러올때)

이때 겟방식으로는 그냥 URL 뒤에 파라미터 값을 넣어주면 되지만 POST 방식으로 값을 넘겨주고

다시 그 파일을 불러와야할때가 있는데 검색하다가 방법을 찾게되었다.

 

  // POST로 넘길 변수값 설정
  $post_data = http_build_query(
    array(
     'con_data' => $변수1,
     'pidx' => $변수2,
     'f_txt1' => $변수3,
    )
  );

//
  $context = stream_context_create(
  array(
   'http' => array(
     'method'  => 'POST', 
     'header'  => 'Content-type: application/x-www-form-urlencoded',
     'content' => $post_data,
     'timeout' => 30,
   ), 
  ));
  
  $body = file_get_contents("http://domain/file.php", false, $context);

위와 같은 방법으로 php파일에 post 값을 넘겨주고 다시 그 파일을 불러 올 수 있다.

Comments