Question

I'm testing embed.ly and use a concatenated comma separated link string to request details for multiple links with one request.

Something like:

http://api.embed.ly/1/oembed?key=_key_&urls=url1,url2,url3

The results includes some details about the link like description, title, thumbnail etc.

Sometimes there is no url field. In most cases there is one. This field contains the url and is, as far as i can tell, identical to the url from the requeststring.

I used this url field like a foreign key and i need to clearly identify the results.

This is the function I use to load the json data from embed.ly. I just loop over an array that contains some urls extracted in another function and implode() them to a querystring.

After recieving the json I check for errors and if there are errors I restore the links bbcodes from rich html to a basic link tag.

But without the used url in the sendback data how should I identify the link the data belongs to?

private function getOPGdata(){

    $linklist = array();

    foreach ( $this->_extractions['ogp'] as &$v ) {

        $linklist[] = urlencode( $v );

    }

    $apicall = $this->_embedkey . implode( ',', $linklist );
    $data    = $this->file_get_contents_curl( $apicall );
    $data    = json_decode( $data, true );

    // we replace ogp tagged links if we don't get results
    // and reverse them to normal links
    $replace     = array();
    $replaceWith = array();

    unset( $apicall );
    unset( $linklist );
    unset( $this->_extractions['ogp'] );
    foreach ($data as &$v) {

        // check for errors
        if( isset( $v['error_code'] ) ){

            $replace[]     = '[ogp]'.$v['url'].'[ogp]';
            $replaceWith[] = '[url]'.$v['url'].'[url]';

        }

        else {

            $r = array(
                'provider_url'  => $v['provider_url'],
                'provider_name' => $v['provider_name'],
                'title'         => $v['title'],
                'description'   => $v['description'],
                'thumbnail_url' => $v['thumbnail_url'],
                'html'          => $v['html'],
                'type'          => $v['type']
            );

            $this->setOPGdata( $v['url'], $r );

        }

    }

    // finally remove links with no results
    $this->_text = str_replace( $replace, $replaceWith, $this->_text );

    return true;

}

In embedly.js they do this to keep track of the results:

  // Put everything into batches, even if these is only one.
  var batches = batch(valid_urls, options.batch), self = this;

  // Actually make those calls.
  $.each(batches, function(i, batch){
    $.ajax({
      url: self.build(method, batch, options),
      dataType: 'jsonp',
      success: function(data){
        // We zip together the urls and the data so we have the original_url
        $.each(zip([batch, data]), function(i, obj){
          var result = obj[1];
          result.original_url = obj[0];
          result.invalid = false;
          keeper.notify(result);
        });
      }
    });
  });

What does this mean in PHP?

Example with URLs:

  [1]=>
  array(11) {
    ["provider_url"]=>
    string(24) "http://stackoverflow.com"
    ["description"]=>
    string(143) "I'm testing embed.ly and use a concatenated comma separated link string to request details for multiple links with one request. Something like:"
    ["title"]=>
    string(79) "embed.ly Problems to identify results beauser somtimes the url field is missing"
    ["mean_alpha"]=>
    float(191.25)
    ["thumbnail_width"]=>
    int(316)
    ["url"]=>
    string(123) "http://stackoverflow.com/questions/23095192/embed-ly-problems-to-identify-results-beauser-somtimes-the-url-field-is-missing"
    ["thumbnail_url"]=>
    string(78) "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png?v=fde65a5a78c6"
    ["version"]=>
    string(3) "1.0"
    ["provider_name"]=>
    string(13) "Stackoverflow"
    ["type"]=>
    string(4) "link"
    ["thumbnail_height"]=>
    int(316)
  }
  [2]=>
  array(11) {
    ["provider_url"]=>
    string(15) "http://embed.ly"
    ["description"]=>
    string(118) "Embedly allows developers to pull full article text for text analysis or building a read it later type applications..."
    ["title"]=>
    string(27) "Artiicle Tutorial | Embedly"
    ["mean_alpha"]=>
    float(32.7480314961)
    ["thumbnail_width"]=>
    int(399)
    ["url"]=>
    string(38) "http://embed.ly/docs/tutorials/article"
    ["thumbnail_url"]=>
    string(58) "http://embed.ly/static/images/logos/logo_color.png?v=4b245"
    ["version"]=>
    string(3) "1.0"
    ["provider_name"]=>
    string(5) "Embed"
    ["type"]=>
    string(4) "link"
    ["thumbnail_height"]=>
    int(127)
  }
}

And one that comes with no to url:

  [0]=>
  array(14) {
    ["provider_url"]=>
    string(18) "https://vimeo.com/"
    ["description"]=>
    string(316) "Writers' Block is a short film written and directed by Tom Gran and Martin Woolley and produced by WONKY Films with the support of IdeasTap. Set in a prison for criminally poor writers, it follows a gang of cons who get ahold of the script to their own lives and attempt to re-write it in order to make their escape."
    ["title"]=>
    string(14) "Writers' Block"
    ["author_name"]=>
    string(12) "WÖNKY Films"
    ["height"]=>
    int(720)
    ["thumbnail_width"]=>
    int(1280)
    ["width"]=>
    int(1280)
    ["html"]=>
    string(389) ""
    ["author_url"]=>
    string(27) "http://vimeo.com/wonkyfilms"
    ["version"]=>
    string(3) "1.0"
    ["provider_name"]=>
    string(5) "Vimeo"
    ["thumbnail_url"]=>
    string(46) "http://i.vimeocdn.com/video/468861310_1280.jpg"
    ["type"]=>
    string(5) "video"
    ["thumbnail_height"]=>
    int(720)
  }
Was it helpful?

Solution

The results are always passed back in the same order that they are passed in. You should be able to do a for loop on your request URLs and just match the results by index in the response array. You could do something like the following to make a map of request URLs to responses.

$urls = array(
  "http://www.google.com",
  "http://www.yahoo.com"
);

$results = get_embedly_results($urls)
$resultMap = array();

for ($i = 0; $i < count($urls); ++$i) {
  resultMap[$urls[$i]] = $results[$i];
}

This is untested code and my PHP is very rusty, but it should give you the general idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top