Jump to content

Upload video via url and api


Liam88
 Share

Recommended Posts

Looking for some advice.

I need to create a page, insert some basic info, create a repeater and save either a video or image.

This is where I'm at so far.

I'm finding that the repeater doesn't save. So when I access the main page after the process, there is no repeater inside.

The logging gets stuck on video uploading to repeater

This is an example url - https://video-lhr6-2.xx.fbcdn.net/v/t42.1790-2/359544611_1299622917599542_1081641833156909957_n.mp4?_nc_cat=104&ccb=1-7&_nc_sid=c53f8f&_nc_ohc=v3DnXymDRrMQ7kNvgGY2g0m&_nc_ht=video-lhr6-2.xx&_nc_gid=Avf0ALrpn8YGUKRdqZmpwGi&oh=00_AYCqXW4qteSLwgQKol8SbVAgvR-O3-9R1QVqmDjjZwiUjw&oe=66ECD5B4

Any advise would be welcomed.

// Continue handling the data if everything is present
$mediaUrl = $data['media_url'];
$mediaType = $data['media_type'];
$description = isset($data['description']) ? $data['description'] : '';

$title = 'Test Ad Title';

try {
    // 1. Create the main page
    $page = new Page();
    $page->template = 'asset'; // Use the correct template
    $page->parent_id = 1029;  // Adjust this ID for the correct parent page
    $page->title = $title;
    $page->content_type = $mediaType; // Assuming 'content_type' is a valid field
    $page->save();

    // Log the creation of the page
    $log->save('save-ad', 'Created new page with title: ' . $title);

    // 2. Create a repeater item for the "content" repeater field
    $log->save('save-ad', 'Creating a new repeater item.');
    $contentRepeater = $page->content->getNew(); // Create a new repeater item

    if (!$contentRepeater) {
        $log->save('save-ad', 'Error: Failed to create new repeater item.');
        echo json_encode(['status' => 'error', 'message' => 'Failed to create repeater item.']);
        exit;
    }

    // 3. Handle media file and add it to the correct field in the repeater
    $filename = basename($mediaUrl); // Extract the filename from the URL

    // Prepare the file to be added
    if ($mediaType == 'video') {
        $log->save('save-ad', 'Adding video file to repeater.');
       
        // Add the file directly to the file field 'video_1'
        $contentRepeater->video_1->add($mediaUrl); // Assuming 'video_1' is the field
        $log->save('save-ad', 'Video file added successfully.');
    } elseif ($mediaType == 'image') {
        $log->save('save-ad', 'Adding image file to repeater.');

        // Add the image directly to the image field 'image_1'
        $contentRepeater->image_1->add($mediaUrl); // Assuming 'image_1' is the field
        $log->save('save-ad', 'Image file added successfully.');
    } else {
        $log->save('save-ad', 'Error: Unsupported media type: ' . $mediaType);
        echo json_encode(['status' => 'error', 'message' => 'Unsupported media type: ' . $mediaType]);
        exit;
    }

    // 4. Save the repeater item
    $log->save('save-ad', 'Saving repeater item.');
    $contentRepeater->save();

    // 5. Add the repeater item to the main page
    $page->content->add($contentRepeater);  // Add the repeater item to the page

    // 6. Save the main page with the repeater
    $page->save();
    $log->save('save-ad', 'Repeater item added to page and page saved successfully.');

    // Log success
    $log->save('save-ad', 'Media saved successfully to page.');
    echo json_encode(['status' => 'success', 'message' => 'Ad saved successfully!']);

 

Link to comment
Share on other sites

A new page needs to be saved before you can add files or images to it. So try saving the new repeater page after you create it:

// ...
$contentRepeater = $page->content->getNew();
$contentRepeater->save();
// ...

I suggest you install TracyDebugger because it would have shown you a error message that explains the problem when you tried to add a file to an unsaved repeater page.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...