Help API xenforo attachment

hunghung

Private
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
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
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
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
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
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
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
 

hunghung

Private
- 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
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
 

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

XenForo 1 XenForo 2
Translate by PVS

Hỗ trợ trực tuyến

Support Quảng cáo
Mr. San (PVS)

Skype chat, instant message

Mr. Tuấn (Blue)

Skype chat, instant message

Mr. Tuấn
0988 488 096
Mr. Trọng
0906 081 284
kinhdoanh@vnxf.vn

Nhà Tài Trợ

Mút Xốp Không Gian
pallet Thịnh Phát
Điện Lạnh Thịnh Phát
Top