함수로 만들어 놓고 쓰면 좋을듯 싶다.
<?php
// GET 방식 함수
function get($url, $params=array())
{
$url = $url.'?'.http_build_query($params, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// get함수 호출
get('http://itfresh.tistory.com', array('param1'=>'value1', 'param2'=>'value2'));
// POST 방식 함수
function post($url, $fields)
{
$post_field_string = http_build_query($fields, '', '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
curl_setopt($ch, CURLOPT_POST, true);
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
// post함수 호출
post('http://itfresh.tistory.com', array('field1'=>'value1', 'field2'=>'value2'));
?>
'개발 > php' 카테고리의 다른 글
php 배열 날짜별로 sort 정렬 (0) | 2021.01.19 |
---|---|
php http or https 체크하여 https로 강제 링크 (0) | 2021.01.19 |
php 카카오 로그인 예제 (0) | 2021.01.19 |
php array_pop()이란 예제 (0) | 2021.01.19 |
"continue" targeting switch is equivalent to "break". error fix (0) | 2021.01.19 |
댓글