CMS/프레임워크 | 사용안함 |
---|---|
개발 언어 | PHP 7.2 |
티스토리에 글을 좀 편하게 자동으로 쓰려고 시도하다가
글 작성은 다 되는데 이미지 파일 첨부까지 해보려는데 쉽지 않네요.
일단 제가 사용한 소스에
public function post_attach ($parameters) {
$data = array();
$parameters[CURLOPT_HTTPHEADER] = array('Content-Type'=>'multipart/form-data');
$parameters[CURLOPT_POST] = true;
// set access_token
if (isset($this->access_token)) {
$data['access_token'] = $this->access_token;
$data['output'] = 'json';
}
if (is_file($parameters['uploadedfile'])) {
$mime_type = mime_content_type($parameters['uploadedfile']);
$cf = curl_file_create($parameters['uploadedfile'],$mime_type,pathinfo($parameters['uploadedfile'],PATHINFO_BASENAME));
$data['uploadedfile'] = $cf;
}
$data['blogName'] = $parameters['blogName'];
unset($parameters['blogName']);
unset($parameters['uploadedfile']);
$parameters[CURLOPT_POSTFIELDS] = $data;
$response = $this->get_content_curl($this->api_url.'apis/post/attach',$parameters);
$data = array();
if ($response['response']['http_code'] == 200) {
$data = $response['html'];
}
return $data;
}
위와 같은 함수 제공하고 있습니다.
* 파일 첨부 API
* http://www.tistory.com/guide/api/post#post-attach
*
* @param array $parameters
* string $parameters['blogName'] xxx.tistory.com 의 xxx
* string $parameters['uploadedfile'] multipart file data
*
* @return array $data
*/
위와 같이 사용해야 하구요.
api 메뉴얼
------------------------------------------
https://tistory.github.io/document-tistory-apis/apis/v1/post/attach.html
POST https://www.tistory.com/apis/post/attach? access_token={access-token} &blogName={blog-name} [uploadedfile]
기본 매개변수를 제외한 매개변수는 다음과 같습니다.
blogName: Blog Name 입니다.
uploadedfile: 업로드할 파일 (multipart/form-data)
---------------------------------
제가 사용하려는 이미지가 외부의 이미지입니다.
$productImage ='url';
$parameters = array();
$parameters['blogName'] = 'https://***.tistory.com/';
$parameters['uploadedfile'] = $productImage;
$tistory_api->set_api(array('access_token'=>$_SESSION['access_token']));
$data = $tistory_api->post_attach($parameters);
저 굵은 색 부분이 저희 서버쪽으로 가져와서 뭔가 형태를 바꿔줘야 할 것 같은데 단서를 못찾겠네요.
저희 서버에 임시 파일 형태로 가동된 결과물이 $parameters['uploadedfile'] 에 담기도록 해야 할 것 같다는 것 까지만 추측하고 있습니다.
댓글 9
$parameters['blogName'] = 'https://***.tistory.com/';
https://tistory.github.io/document-tistory-apis/GLOSSARY.html#blog-name
"블로그를 구분하는 식별자로 사용되며 티스토리 주소 xxx.tistory.com에서 xxx를 나타냅니다."
라고 되어 있는데 이부분도 확인해 보셨나요? *** 부분만 적으셔야 할것 같은데요.
=====
API 문서를 보면 access_token과 blogName은 GET 파라메터로, 나머지 실제 업로드할 파일은 multipart/form-data 형태로 POST 본문에 첨부해야 하는것 같습니다.
지금 첨부를 시도하고 있어요.
함수에서 if (is_file($parameters['uploadedfile'])) 이런부분이 등장하는거 보니 외부 이미지 url로는 안되고 저희쪽으로 가져와서 뭔가
$parameters['uploadedfile'] multipart file data
이런 형식으로 해야 한다고 말하는 거 같습니다.
이부분을 어찌 처리하는지 잘 모르겠어서요.
그럴땐 구글에 검색해 보면 되죠.
문서가 저렇게만 제공이 되면 사실 추측해 보는 방법밖엔 없습니다. 대소문자를 바꾼다던가 전송 방식을 바꾸는 식으로요.
네.
$parameters['uploadedfile'] multipart file data
함수를 만들어 배포한 분이 작성한 이것의 의미를 실행하고 싶은데요.
multipart file data 의 형식으로 사용하라고 했으니
$parameters['uploadedfile'] ='multipart file data';
이렇게 사용하라는 것 까지는 이해를 했습니다.
그럼
$productImage = file_get_contents('외부 이미지url');
이렇게 가지고 온 이미지를 multipart file data 형식으로 만들어주면 되는건지..
그리고 그게 맞다면 형식으로 만들수 있는 방법이 궁금합니다. 검색을 해도 많은 문서가 나와서 그리고 제가 하려는방향이 맞는지도 잘 모르고 해서 질문을 통해 제 생각 중 잘못된 것을 제거해 가는 작업이 필요해 보입니다.
그리고 아리송한건
public function post_attach ($parameters)
함수에 보면
if (is_file($parameters['uploadedfile'])) {
$mime_type = mime_content_type($parameters['uploadedfile']);
$cf = curl_file_create($parameters['uploadedfile'],$mime_type,pathinfo($parameters['uploadedfile'],PATHINFO_BASENAME));
$data['uploadedfile'] = $cf;
}
이런부분이 등장하는데 그냥 이미지 url 넣으면 된는건지 좀 햇깔리기도 하네요.