네이버 블로그 API 이미지까지 업로드

네이버 블로그 API 이미지까지 업로드


제목보면 뭔가 이상하지만 삽질의 결과물로 인한것이라..../먼산 


서버에 있는 데이터를 네이버 블로그API를 이용하여 글을 올리게 되면 이미지는 서버에 저장된 채로 글만 올라갔었는데 


이것저것 뒤적 뒤적 하다가 어찌어찌 연결되었습니다./먼산 


사용하시는 분들에게 도움이 되었으면 좋겠네요 =ㅅ=ㅋ 


참고로 category의 지정시 없는걸로 하면 아마도 기본 설정으로 저장되는걸로 알고 있습니다. 

(물론 확실하지 않아요/먼산) 


기타 질문도 받습니다. 


http://www.xmlrpc.com/spec 

여기서 xmlrpc.inc 파일 받는거 잊으시면 안됩니다. 


P.S. 제가 작업한 서버쪽은 이상하게 name time out이 자주 뜹니다. 

curl 문제인거 같은데 제가 서버쪽을 잘 몰라서...../먼산 

아마 속도 때문에 그런거 같으니 문제 되시는 분들은 timeout을 늘려주세요< 


P.S.2 지금보니 일부 쓸모없는 echo가 남아있네요.... 어차피 주석처리 되어 있지만 테스트 하실때 써먹../퍽!

[ ▼ 참고 Source ]


include "xmlrpc/lib/xmlrpc.inc" ; // 이 부분은 다 아실테니...
$rs << DB에서 불러온 데이터 입니다.

$tit = $rs[0][title] ;
$tex = $rs[0][text] ;
$loof_chk = 0 ;

$url = "https://api.blog.naver.com/xmlrpc";
$blogid = "ID"; << 어차피 같은 네이버 아이디니...../먼산
$id = "ID";
$pw = "api키 값"; 블로그에서 할당받은 API KEY

// 네이버 api 이미지 업로드 함수
function upload_image( $url , $blogid , $id , $pw , $tit , $tex ) {

	$api_url = $url ;
	$blog_user = $id ;
	$blog_passwd = $pw ;
	preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", $tex , $out ) ;
	preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/ui", $tex , $out2 ) ;
	$cmm = count($out[1]) ;
//	echo "".count($out[1]) ;
	if ( $cmm > 0 )
	{
		for ( $i = 0 ; $i < $cmm ; $i++ )
		{
			$im = "http://서버주소".$out[1][$i] ;

			$imgbit = file_get_contents($im, FILE_BINARY);
			$img = new xmlrpcval(
			array (
			'bits' => new xmlrpcval($imgbit, 'base64'),
			'type' => new xmlrpcval('image/jpeg', 'string'),
			'name' => new xmlrpcval(basename($im), 'string')
			), 'struct');

			$c = new xmlrpc_client($api_url);
//			$c->debug = true; // Uncomment this line for debugging info
			$c->setSSLVerifyPeer(false);

			$x = new xmlrpcmsg("metaWeblog.newMediaObject");
			$x->addParam(new xmlrpcval($blog_user, 'string'));
			$x->addParam(new xmlrpcval($blog_user, 'string'));
			$x->addParam(new xmlrpcval($blog_passwd, 'string'));
			$x->addParam($img);

			$c->return_type = 'phpvals';
			$r =$c->send($x, 3, 'https');
			$message->request_charset_encoding = "UTF-8";
			$tex = str_replace($out2[1][$i], $r->val[url] , $tex ) ;
			if ( $r->val[errno] == 0 and $r->errno != 8 ) { $loof_chk++ ; }
//			echo $tex."".$i."loof_chk = ".$loof_chk."" ;
//			print_r($r); // 결과 부분입니다. 에러체크용으로도 사용할 수 있습니다.
//			echo "" ;

			if ( ( $r->errno != 8 and $r->val[errno] == 0 ) and ( $i == ( $cmm - 1 ) and $loof_chk == $cmm ) )//이미지가 다량존재시 하나라도 업로드 실패했을 경우에 대비한 if 문입니다.
			{ return m3_naver_post( $url , $blogid , $id , $pw , $tit , $tex , $_GET['position']) ; } //$_GET['position']은 전 페이지에서 카테고리설정을 위해 받는 부분입니다.
			else if ( $i == ( $cmm - 1 ) ) { echo "<script>alert('업로드에 실패하였습니다.');document.getElementById('emd').value = 'fail';</script>" ; }
		}
	}
	else { return m3_naver_post( $url , $blogid , $id , $pw , $tit , $tex , $_GET['position']) ; }
}

// 네이버 api 포스팅 올리는 함수
function m3_simple_metaweblog($url, $blogid, $id, $pw, $title, $description, $category, $publish=true) {
	$GLOBALS["xmlrpc_internalencoding"] = "UTF-8";
	$client = new xmlrpc_client($url);
	//$client->setdebug(1);
	$struct = array(
		'title' => new xmlrpcval($title, "string"),
		'description' => new xmlrpcval($description, "string"),
		'categories' => new xmlrpcval($category, "string") 
		);
	$message = new xmlrpcmsg("metaWeblog.newPost", array(
		new xmlrpcval($blogid, "string"),
		new xmlrpcval($id, "string"),
		new xmlrpcval($pw, "string"),
		new xmlrpcval($struct, "struct"),
		new xmlrpcval($publish, "boolean")
		)
	);
	return $response = $client->send($message);
}

// 네이버 직접 포스팅 함수
function m3_naver_post( $url , $blogid , $id , $pw , $title, $description, $category) {
	global $api;

	return $response = m3_simple_metaweblog($url, $blogid, $id, $pw, $title, $description, $category);
}

$return = upload_image( $url , $blogid , $id , $pw , $tit , $tex ) ;
?>