<?php
class Log {
public static function debug($str) {
print "DEBUG: " . $str . "\n";
}
public static function info($str) {
print "INFO: " . $str . "\n";
}
}
function Curl($url, $post_data, &$http_status, &$header = null) {
Log::debug("Curl $url JsonData=" . $post_data);
$ch=curl_init();
// user credencial
curl_setopt($ch, CURLOPT_USERPWD, "username:passwd");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
// post_data
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
if (!is_null($header)) {
curl_setopt($ch, CURLOPT_HEADER, true);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
Log::debug('Curl exec=' . $url);
$body = null;
// error
if (!$response) {
$body = curl_error($ch);
// HostNotFound, No route to Host, etc Network related error
$http_status = -1;
Log::error("CURL Error: = " . $body);
} else {
//parsing http status code
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!is_null($header)) {
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
} else {
$body = $response;
}
}
curl_close($ch);
return $body;
}
$url = "http://requestb.in/1h1tct81";
$json = "{\"name\" : \"UserName\", \"age\" : 12 }";
$ret = Curl($url, $json, $http_status);
var_dump($ret);
?>
출처 https://www.lesstif.com/pages/viewpage.action?pageId=17105778
'Program > PHP' 카테고리의 다른 글
RSS 피드를 이용해 인기 급상승 구글트렌드 검색 가져오기 (0) | 2017.11.27 |
---|---|
[PHP]json_encode 유니코드 오류 php 5.3버전 (2) | 2017.09.21 |
[PHP]curl을 이용한 REST api 사용예제 (1) | 2017.07.19 |
PHP FTP 함수 (0) | 2017.03.08 |
mysql 테이터베이스, 기존테이블 복사하기 (0) | 2017.03.06 |
댓글