Get specific result using json_decode ()

0

It turns out that I have a php code to extract the url from the images of a tumblr blogs that is this:

    $URL = "http://api.tumblr.com/v2/blog/luxury-boobs.tumblr.com/posts/?api_key=fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4&notes_info=true&type=photo&offset=##DESDE##";
    $A = json_decode(file_get_contents($URL),true);

 foreach($A['response']['posts'] as $P){
    $rawContent = file("datost.txt"); //Abrimos el archivo datos.txt
    $content = implode(" ",$rawContent);//Ya tenemos la cadena en memoria

     //Verificamos que el id no exista 
     if (preg_match($P['id'],$content,$arrMatches)){
     echo "el id de la imagen ya existe  <br/>\n";}
     else { 
     $data =$P['photos'][0]['original_size']['url'];

     echo  $data"<br/>\n"; // Muestra el resultado

     $file = fopen("datost.txt", "a"); // si no existe almacena el id 
     fwrite($file, PHP_EOL . $P['id']);
     fclose($file);}
     } 

It works but when I see the result it gives me more than one image and more than one id. So I can not control the images.

What I'm looking for is to get a url and its id only and store the id so as not to repeat the same image. Thanks

This is the result of the variable

  

$ data

http://68.media.tumblr.com/4a759aca11f155398e585a98a178d3b6/tumblr_oinb2sebHz1rqxzy4o2_1280.jpg
http://68.media.tumblr.com/bbfae2d325de64cef42f0360e402fec7/tumblr_o4pf2xQDml1rfzfuxo1_1280.jpg
http://68.media.tumblr.com/766d03fc98f88fbf2a87afc7a3eccf91/tumblr_obdq641SiC1u9uov7o1_400.gif
http://68.media.tumblr.com/c787d8a5849eda01f770c2818d229dbc/tumblr_o291moFQye1swnrm8o1_500.jpg
http://68.media.tumblr.com/tumblr_ljff8ppblk1qf0gljo1_500.jpg
http://68.media.tumblr.com/2f108602f74097a62b59669efbe35b37/tumblr_o278twxICE1rqxzy4o1_1280.jpg
http://68.media.tumblr.com/016ed8a1f0b807a7364e260a4f44a03b/tumblr_o278pztiBw1rqxzy4o1_1280.jpg
http://68.media.tumblr.com/8c250f6fc294556c879b8deb02811dce/tumblr_o278ltW8rE1rqxzy4o1_1280.jpg
http://68.media.tumblr.com/20a76988729ea034985e38fdeed16fbe/tumblr_o278icrLID1rqxzy4o1_1280.jpg
http://68.media.tumblr.com/b0f4664785dc2636590244b31d732a25/tumblr_o278d6osPq1rqxzy4o1_1280.jpg
http://68.media.tumblr.com/tumblr_lnnaoh3qez1qltduvo1_1280.jpg
http://68.media.tumblr.com/238dfd7ca452c4470d9caea8e9703cad/tumblr_mnp30uenFu1stfbuso2_1280.jpg
http://68.media.tumblr.com/1323a2e51665501b8c9dacf0bbd305f0/tumblr_mpbnc9MiEi1rwp671o1_1280.jpg
http://68.media.tumblr.com/cd7c34f54af83667ff480823b81224cf/tumblr_o25hvhSwXJ1rqxzy4o1_500.gif
http://68.media.tumblr.com/09946a1cc8b10d7024b223e064acfb4e/tumblr_o1poh3XOmW1v5em5ho1_400.gif
http://68.media.tumblr.com/e2f636cfe2779d7cd6347fca088f1ebd/tumblr_o1gzj8oKrf1ueytcko1_500.gif
http://68.media.tumblr.com/c0c156a74b6a08049896cd9bb5e63a99/tumblr_o23nfgsVKs1rqxzy4o1_400.gif
http://68.media.tumblr.com/ccd2751aaf79d10d8371ed32db4f06e2/tumblr_o23n99p9Hk1rqxzy4o1_400.gif
http://68.media.tumblr.com/0b26721187fd993144df7b224457f6e8/tumblr_o23n7pbUHt1rqxzy4o1_400.gif
http://68.media.tumblr.com/539ddf3e6d35faf9f538f309d9c90bc8/tumblr_nx7h26U1cT1shhqv9o1_500.jpg

To get the id:

$data =$P['id'];;

     echo  $data"<br/>\n"; // Muestra el resultado

This is the result:

139221086730
139220931760
138314532060
137464162310
137463383420
135372993005
135372890025
134975704250
134905671980
134844575050
134843185540
134843119040
134705537650
134705454155
129882281635
113145782480
113145573875
111737765245
103800994925
103182657345
    
asked by BotXtrem Solutions 26.09.2017 в 20:52
source

2 answers

1

I still do not understand what you want to achieve. But I intuit that it would be better to do it from jQuery instead of sending everything to the server.

For example, here we get two arrays, one with the ids of the posts, and another with the URLs of the photos.

Starting from there, if you only want to obtain a post for its id and the url of the photos of it, it would be a matter of filtering, knowing the value of the id.

The code shows how you get for example the URL of a photo of a post knowing in advance the id of the post:

Foto del post 165773447226: 
https://68.media.tumblr.com/27f0bf6ccf0aae9d5c649e4d7823a09c/tumblr_owwn5vuJue1tobld4o3_1280.gif

If you only want this part:

tumblr_owwn5vuJue1tobld4o3_1280.gif

It would be very easy to obtain it.

I hope it serves you.

var url = "https://api.tumblr.com/v2/blog/luxury-boobs.tumblr.com/posts/?api_key=fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4&notes_info=true&type=photo&offset=##DESDE##";


var request = $.ajax({
  url: url,
  dataType: "json"
});

request.done(function(data) 
{
  arrPosts = data.response.posts;
  arrPostsIds = new Array();
  arrPhotoUrls = new Array();

  for (k in arrPosts) 
  {
    arrPostsIds.push(arrPosts[k].id);

    for (k1 in arrPosts[k].photos) 
    {
        arrPhotoUrls.push(arrPosts[k].photos[k1].original_size.url);
    }
  }


  /*Prueba para un  post específico*/
    intUnPostId=165773447226;
    strUnaFoto="";
    for (k in arrPosts) 
    {
      if (arrPosts[k].id==intUnPostId)
      {
        for (k1 in arrPosts[k].photos) 
        {
           strUnaFoto=arrPosts[k].photos[k1].original_size.url;
        }
      }
   }


  console.log("Foto del post "+intUnPostId+": "+strUnaFoto);

  console.log("ARREGLOS DE POSTS Y DE FOTOS");
  console.log(arrPostsIds);
  console.log(arrPhotoUrls);

});

request.fail(function(jqXHR, textStatus) 
{
  alert("La petición Ajax falló: " + textStatus);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
answered by 27.09.2017 / 13:58
source
1

I see that when performing the query some index values are not numeric. Add the following control when recording the index.

if(is_numeric($P['id'])){
            echo $P['id']  . "<br>";
                $file = fopen("datost.txt", "a"); 
                fwrite($file, PHP_EOL . $P['id']);
                fclose($file);
}

The id that you. it gets from post , not from the image.

  id    165626628606
  post_url  "https://luxury-boobs.tumblr.com/post/165626628606/justperfectness-lucies-perfect-knockers"

I do not know the site but the images may be repeated, so not the id of the post .
I did not get repeated values.

    
answered by 27.09.2017 в 02:17