Strange bug with creating pages with a lodash in the UID

Not sure if this actually is a bug, or if I’m doing something fishy.

When query’ing the Instagram API, I get back a bunch of JSON, containing an array with all my pictures.
All pictures have a unique id field, which is the picture of the id, followed by the user’s id.
E.g. 1416005213411718757_1452701316

When I loop over all my pictures, and try to create a page with the image ID, the first one always succeeds. Starting from the second, it fails and tells me Caught exception: The page UID exists.

The created UID for the first page however gets turned into 1425394031323664471-1452701315, looking like the lodash is automatically replaced by a hyphen. But for some reason, the second page won’t get created.

Any ideas?

Could you provide your code for creating the pages, please?

Sure!

<?php
if (kirby()->request()->ajax()) {

  try {
    $url = 'https://api.instagram.com/v1/users/self/media/recent/?access_token=' . $site->content()->get('instagram_token')->value() . '&count=10';

    $curl = curl_init();

    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url,
      CURLOPT_USERAGENT => 'Codular Sample cURL Request',
      CURLOPT_SSL_VERIFYPEER => false
    ));

    // Send the request & save response to $resp
    $resp = curl_exec($curl);

    foreach (json_decode($resp, true)['data'] as $image) {
      try {
        $uri = 'fotos/' . $image['id'];
        $page->create($uri, 'photo', array(
          'title' => $image['caption']['text'],
          'ig-id' => $image['id'],
          'imageUrl' => $image['images']['standard_resolution']['url'],
          'tags' => $image['tags']
        ));
      } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
    }
  } catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
  }

  echo 'Import successful!';
}
else {
  header::status('404');
}

This should work:

foreach (json_decode($resp, true)['data'] as $image) {
      try {
        
        page('fotos')->children()->create($image['id'], 'photo', array(
          'title' => $image['caption']['text'],
          'ig-id' => $image['id'],
          'imageUrl' => $image['images']['standard_resolution']['url'],
          'tags' => $image['tags']
        ));
      } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
    }

You need to create the subpages as children of the page. $page->create() can’t be used in a loop like this.

Getting the same exception again…

For reference, this is my current fix

try {
        $uri = 'fotos/' . explode('_', $image['id'])[0];
        $page->create($uri, 'photo', array(
          'title' => $image['caption']['text'],
          'ig-id' => $image['id'],
          'imageUrl' => $image['images']['standard_resolution']['url'],
          'tags' => trim(array_reduce($image['tags'],"mergeTags"))
        ));
      } catch (Exception $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
    }

I just strip away the last part of the ID. The last part of the ID is actually always the same: the Instagram user’s ID.
So maybe it triggers on that?

You could also try str::slug($image['id']). Maybe it just doesn’t like the underscore. The repeating part shouldn’t be the problem as long as the UID is unique :thinking:

Still getting the error :confused:

Could you provide an excerpt of the json data for testing?

Here you go

God, how stupid of me :rage:. The first number is regarded as a visibility flag, of course, so each page does indeed have the same UID if you use the complete $image['id'].

If you want to use `$image[‘id’], you would have to prepend a string, e.g.

foreach (json_decode($file, true)['data'] as $image) {
      $uid = 'image-' . $image['id'];

      try {
          page('fotos')->children()->create($uid, 'photo', array(
            'title' => $image['caption']['text'],
            'ig-id' => $image['id'],
            'imageUrl' => $image['images']['standard_resolution']['url'],
            'tags' => $image['tags']
          ));
        } catch (Exception $e) {
          echo 'Caught exception: ',  $e->getMessage(), "\n";
        }
      }

Oh darn, it was too obvious indeed!

Thanks for the help though, thought it was really something I was doing wrong, but just overlooked the ID part… :sweat_smile: