XF 2 [PAID] Need someone to fix a PHP script that imports Avatars through XF API

BubbaLovesCheese

Active member
I need someone to help fix a PHP script that imports Avatars (located in a file folder) and upload them to Xenforo through the API.

The script is partially done, but does not function.

It uses a csv file to match the user id, and the avatar file name.

The php script, avatar folder, and csv file, will all be located on the server.

csv file example:

Code:
852,/avatars/2521.jpg
79518,/avatars/109779.gif
121826,/avatars/167575.png

This is the non-functioning PHP script that needs fixing:

PHP:
<?php

//  1.  file path         $filePath
//  2.  XF-Api-Key        $apiKey    It must be Super user key
//  3.  XenForo base URL  $url       The primary URL. Do not include a trailing "/"
//  4.  csv file should be : userid, avatar file path

$filePath = "avatar-doc.csv";                      //file path
$apiKey   = 'X9BSzlay4helZNOfiWczSejt-bd5ceKL';    //XF-Api-Key
$boardUrl   = 'https://test-forum.mysite.com';   //XenForo base URL - Without the trailing "/"

//Check if csv file is readable
if (($handle = fopen($filePath, "r")) !== FALSE)
{
  //Read file line by line till end
  while (($data = fgetcsv($handle)) !== FALSE)
  {
      {
          //Upload the avatar for each user

          //set header
          $headers = array(
           'Content-type: multipart/form-data',
           'api_bypass_permissions: 1',         // bypass the context user's permissions
           'XF-Api-Key: '.$apiKey
          );

          $file = $data[1];
          $mime = mime_content_type($file);
          $info = pathinfo($file);
          $name = $info['basename'];
          $file = new CURLFile($file, $mime, $name);
         
          $post = http_build_query($post);

          //set post body
          $post['avatar'] = $file;

          //set url
          $url = $boardUrl. "/api/users/" . $data[0] . "/avatar";
     
          //post request for creating user
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_POST, true);
          curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          $json = curl_exec($ch);

          // convert json response to array
          $response = json_decode($json, 'true');

          if(isset($response['success']) AND $response['success'] == 1)
          {
            //message for success
            echo "Successfully created Avatar for " . $data[0] . "\n";
          }
          else
          {
            //message for failure due to some reason
            echo "There was an ERROR adding Avatar for " . $data[0] . "\n";
          }
        }
    }
}

If anyone is willing to take on this project, please PM me with your fee.

Thank you!
 
Back
Top Bottom