Help API xenforo attachment

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
Em đang phát triển để bắn data sang xfr = api, hiện tại đã tạo được thread và đang vướng phần tạo attachment, đọc hết các document mà em mãi chưa xử lý được. Bác nào từng phát triển có thể cho em xin 1 mẫu code php được k.
Hiện tại code của em như sau:

Mã:
$file = file_get_contents(file);
$apikey = 'dfhsdg';
$method = CURLOPT_POST;




// tạo mới key :

$data = [

    "type" => 'threads',

    "context" => 'node_id=10',

    "attachment" => $file,

];



// tạo key

$rest_api_url = 'http://exampleurl.vn/api/attachments/new-key';

$result = $this->curl_api($api_key, $rest_api_url, $method, $data);

//

function curl_api ($api_key, $rest_api_url, $method, $data){

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $rest_api_url);

curl_setopt($ch, CURLOPT_ENCODING, '');

curl_setopt($ch, CURLOPT_MAXREDIRS, 10);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

if ($method == CURLOPT_POST) {

    curl_setopt($ch, $method, 1);

}

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$headers = array(

    'Content-Type:  multipart/form-data',

    'XF-Api-Key: ' . $apiKey,

);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);



$result = curl_exec($ch);

curl_close($ch);

return;

}



// tạo mới attachment


$bta = [
                    "key" => $keys,
                    "attachment" => $file
                ];
                $rest_api_url = 'http://examplurl.vn/api/attachments';
                $result = $this->curl_api($api_key, $rest_api_url, $method, $bta);

luôn xuất hiện lỗi :
stdClass Object ( [errors] => Array ( [0] => stdClass Object (
Mã:
 => required_input_missing [message] => Required input missing: type [params] => stdClass Object ( [missing] => Array ( [0] => type ) ) ) ) )
Nếu được cho em xin 1 demo. Cảm ơn mọi người rất nhiều
 

Win

MasterCorporal
Tham gia
15/02/2016
Bài viết
378
Được Like
171
Thay vì chèn attachment mình update file ảnh vào 1 thư mục ở host rồi chèn vào bài ở dạng Bdcode cho tiện

PHP:
// add images to body
if ($images) {
    $new_images = bc_images($images,$title);
    if ($new_images && sizeof($new_images) >0){
        $message .= "
        
        ";
        foreach ($new_images as $key => $value) {
            $message .= '
            [IMG]'.$value.'[/IMG]';;
        }
    }
}
else $new_images = 0;
 

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
Thay vì chèn attachment mình update file ảnh vào 1 thư mục ở host rồi chèn vào bài ở dạng Bdcode cho tiện

PHP:
// add images to body
if ($images) {
    $new_images = bc_images($images,$title);
    if ($new_images && sizeof($new_images) >0){
        $message .= "
       
        ";
        foreach ($new_images as $key => $value) {
            $message .= '
            [IMG]'.$value.'[/IMG]';;
        }
    }
}
else $new_images = 0;
file ảnh của em không cố định bác à, nên khi nào cần sẽ cần upload ảnh qua api, nên em mới dùng attachment để upload file. Mà vẫn chưa xử lý được :(
 

Win

MasterCorporal
Tham gia
15/02/2016
Bài viết
378
Được Like
171
file ảnh của em không cố định bác à, nên khi nào cần sẽ cần upload ảnh qua api, nên em mới dùng attachment để upload file. Mà vẫn chưa xử lý được :(
Không cố định là sao nhỉ? Nếu user upload lên thì sẽ có file upload, nếu là file url sẽ là file url, nếu file trong thư mục sẽ là file.

Còn code upload qua api như sau:
PHP:
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'xxxxxxxxxxxxxx/api/attachments/new-key',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('type' => 'post','context[node_id]' => '2','attachment'=> new CURLFILE('/C:/Users/xxxx/OneDrive/Desktop/197549024_328431145386739_6419394741864947763_n.jpg')),
  CURLOPT_HTTPHEADER => array(
    'XF-Api-Key: xxxxxxxxxxxxxx',
    'XF-Api-User: 13'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Kết quả

Mã:
{
  "key": "1637552988-tPjykk2a0BAGsM2kInjiv",
  "attachment": {
      "attach_date": 1637552988,
      "attachment_id": 223,
      "content_id": 0,
      "content_type": "post",
      "direct_url": "xxxxxxxxxxxxxx/attachments/197549024_328431145386739_6419394741864947763_n-jpg.223/?hash=OGhy9ECgv7ffVb4SGGKNh8mASmF7V73-",
      "file_size": 216847,
      "filename": "197549024_328431145386739_6419394741864947763_n.jpg",
      "height": 1200,
      "is_audio": false,
      "is_video": false,
      "thumbnail_url": "xxxxxxxxxxxxxx/data/attachments/0/223-3a6d97d076719c5354225501eaea3f38.jpg",
      "view_count": 0,
      "width": 923
  }
}
 

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
Không cố định là sao nhỉ? Nếu user upload lên thì sẽ có file upload, nếu là file url sẽ là file url, nếu file trong thư mục sẽ là file.

Còn code upload qua api như sau:
PHP:
<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'xxxxxxxxxxxxxx/api/attachments/new-key',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('type' => 'post','context[node_id]' => '2','attachment'=> new CURLFILE('/C:/Users/xxxx/OneDrive/Desktop/197549024_328431145386739_6419394741864947763_n.jpg')),
  CURLOPT_HTTPHEADER => array(
    'XF-Api-Key: xxxxxxxxxxxxxx',
    'XF-Api-User: 13'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Kết quả

Mã:
{
  "key": "1637552988-tPjykk2a0BAGsM2kInjiv",
  "attachment": {
      "attach_date": 1637552988,
      "attachment_id": 223,
      "content_id": 0,
      "content_type": "post",
      "direct_url": "xxxxxxxxxxxxxx/attachments/197549024_328431145386739_6419394741864947763_n-jpg.223/?hash=OGhy9ECgv7ffVb4SGGKNh8mASmF7V73-",
      "file_size": 216847,
      "filename": "197549024_328431145386739_6419394741864947763_n.jpg",
      "height": 1200,
      "is_audio": false,
      "is_video": false,
      "thumbnail_url": "xxxxxxxxxxxxxx/data/attachments/0/223-3a6d97d076719c5354225501eaea3f38.jpg",
      "view_count": 0,
      "width": 923
  }
}
Em cảm ơn bác nhiều, may quá có bác giúp, em làm được rồi.
Của em bị sai đường dẫn file ảnh nên nó k đẩy đi được.
Một lần nữa cảm ơn bác rất nhiều nhiều!
 
  • Like
Reactions: Win

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
Bác cho em hỏi thêm chút nữa,:
- Cái attachment key khi add vào threads thì mình add kiểu mảng để upp multiple file trong đấy dc k ạ, hay làm cách nào vậy bác.
- Và content khi đẩy sang thì dạng html, sang xenforo nó là bbcode, làm cách nào để nó chuyển đổi được ạ.
 

Win

MasterCorporal
Tham gia
15/02/2016
Bài viết
378
Được Like
171
Bác cho em hỏi thêm chút nữa,:
- Cái attachment key khi add vào threads thì mình add kiểu mảng để upp multiple file trong đấy dc k ạ, hay làm cách nào vậy bác.
- Và content khi đẩy sang thì dạng html, sang xenforo nó là bbcode, làm cách nào để nó chuyển đổi được ạ.
- Khi dùng /new-key sẽ tạo 1 key + upload file đầu tiên
- Sử dụng lại key sinh ra ở new-key để upload thêm các ảnh khác
- Chèn key vào khi /threads

Thử thư viện này html to bdcode https://github.com/vamsiikrishna/html-to-bbcode
 

dcstylexf

Major
Tham gia
24/03/2015
Bài viết
2,006
Được Like
1,615

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
- Khi dùng /new-key sẽ tạo 1 key + upload file đầu tiên
- Sử dụng lại key sinh ra ở new-key để upload thêm các ảnh khác
- Chèn key vào khi /threads

Thử thư viện này html to bdcode https://github.com/vamsiikrishna/html-to-bbcode
nhưng khi chèn vào threads sẽ chỉ chèn được 1 key, nghĩa là threads ấy chỉ có 1 attachment đính kèm bác ạ, nếu dùng update threads để thêm attachent key khác vào đó thì trong doc nó k hỗ trợ bác à. Kiểu em muốn khi tạo threads thì sẽ gán nhiều attachment key vào threads đó.
 

Win

MasterCorporal
Tham gia
15/02/2016
Bài viết
378
Được Like
171
nhưng khi chèn vào threads sẽ chỉ chèn được 1 key, nghĩa là threads ấy chỉ có 1 attachment đính kèm bác ạ, nếu dùng update threads để thêm attachent key khác vào đó thì trong doc nó k hỗ trợ bác à. Kiểu em muốn khi tạo threads thì sẽ gán nhiều attachment key vào threads đó.
1 key nhiều attachment mà bác
 

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
thanks bác nhiều nhiều, bác ở hn cho em xin info, chủ nhật em mời bác cốc cafe :)
 
Sửa lần cuối:
  • Like
Reactions: Win

hunghung

Private
Tham gia
20/11/2021
Bài viết
7
Được Like
2
update nội dung bài viết xem qua đây, dành cho ae về sau phát triển:
 
Sửa lần cuối:

Hướng dẫn sử dụng

XenForo 1 XenForo 2
Translate by PVS

Dịch vụ XenForo của VNXF

Mr. Tuấn

Mobile/Zalo: 0988 488 096

Telegram: bluekpro

Email: [email protected]

Nhà Tài Trợ

Mút Xốp Không Gian
pallet Thịnh Phát
Top Bottom