JSON 데이타 POST 예제 > php

본문 바로가기
사이트 내 전체검색

php

JSON 데이타 POST 예제

페이지 정보

작성자 서방님 댓글 0건 조회 260회 작성일 18-09-19 14:03

본문

<?php
class Log {
	public static function debug($str) {
		print "DEBUG: " . $str . "\n";
	}
	public static function info($str) {
		print "INFO: " . $str . "\n";
	}
	public static function error($str) {
		print "ERROR: " . $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);
?>



사용자 등록 예제
<?php
$user = array("name" => "userId",
	"password" => "userPwd",
	"emailAddress" => "user@example.com",
	"displayName" => "< USER DISPLAY NAME >",
	"notification" => "true");

$json = json_encode($user);
print("$json\n");


$http_status = 0;

$url = "https://jira-host-name/rest/api/2/user/";
$ret = Curl($url, $json, $http_status) ;
var_dump($ret);
?>

댓글목록

등록된 댓글이 없습니다.

Total 612건 8 페이지
게시물 검색

회원로그인

접속자집계

오늘
69
어제
84
최대
1,347
전체
154,440
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.