Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform "javascript/jQuery-like" functions using PHP

I'm trying to move some processing from client to server side.

I am doing this via AJAX.

In this case t is a URL like this: https://itunes.apple.com/us/podcast/real-crime-profile/id1081244497?mt=2&uo=2.

First problem, I need to send a bunch of these URLs through this little function, to just pull out "1081244497" using my example. The following accomplishes this in javascript, but not sure how to make it loop in PHP.

    var e = t.match(/id(\d+)/);
      if (e) {
           podcastid= e[1];

      } else {
           podcastid = t.match(/\d+/);

      }

The next part is trickier. I can pass one of these podcastid at a time into AJAX and get back what I need, like so:

 $.ajax({
          url: 'https://itunes.apple.com/lookup',
          data: {
              id: podcastid,
              entity: 'podcast'
          },
          type: 'GET',
          dataType: 'jsonp',
          timeout: 5000,
          success: function(data) {
             console.log(data.results); 
          },  
      });

What I don't know how to do is accomplish this same thing in PHP, but also using the list of podcastids without passing one at a time (but that might be the only way).

Thoughts on how to get started here?

MAJOR EDIT

Okay...let me clarify what I need now given some of the comments.

I have this in PHP:

 $sxml = simplexml_load_file($url);
    $jObj = json_decode($json);
    $new = new stdClass();  // create a new object
    foreach( $sxml->entry as $entry ) {
        $t = new stdClass();
        $t->id      = $entry->id;
        $new->entries[] = $t;   // create an array of objects
    }
    $newJsonString = json_encode($new);
var_dump($new);

This gives me:

object(stdClass)#27 (1) {
  ["entries"]=>
  array(2) {
    [0]=>
    object(stdClass)#31 (1) {
      ["id"]=>
      object(SimpleXMLElement)#32 (1) {
        [0]=>
        string(64) "https://itunes.apple.com/us/podcast/serial/id917918570?mt=2&uo=2"
      }
    }
    [1]=>
    object(stdClass)#30 (1) {
      ["id"]=>
      object(SimpleXMLElement)#34 (1) {
        [0]=>
        string(77) "https://itunes.apple.com/us/podcast/real-crime-profile/id1081244497?mt=2&uo=2"
      }
    }
  }
}

What I need now is to pull out each of the strings (the URLs) and then run them through a function like the following to just end up with this: "917918570,1081244497", which is just a piece of the URL, joined by a commas.

I have this function to get the id number for one at a time, but struggling with how the foreach would work (plus I know there has to be a better way to do this function):

$t="https://itunes.apple.com/us/podcast/real-crime-profile/id1081244497?mt=2&uo=2";
$some =(parse_url($t));
$newsome = ($some['path']);
$bomb = explode("/", $newsome);
$newb = ($bomb[4]);
$mrbill = (str_replace("id","",$newb,$i));
print_r($mrbill);
//outputs 1081244497
like image 382
jonmrich Avatar asked Jun 30 '26 15:06

jonmrich


1 Answers

find match preg_match() and http_build_query() to turn array into query string. And file_get_contents() for the request of the data. and json_decode() to parse the json responce into php array.

in the end it should look like this.

$json_array = json_decode(file_get_contents('https://itunes.apple.com/lookup?'.http_build_query(['id'=>25,'entity'=>'podcast'])));
if(preg_match("/id(\d+)/", $string,$matches)){
    $matches[0];
}

You may have to mess with this a little. This should get you on the right track though. If you have problems you can always use print_r() or var_dump() to debug.

As far as the Apple API use , to seperate ids

https://itunes.apple.com/lookup?id=909253,284910350

you will get multiple results that come back into an array and you can use a foreach() loop to parse them out.

EDIT

Here is a full example that gets the artist name from a list of urls

$urls = [
    'https://itunes.apple.com/us/podcast/real-crime-profile/id1081244497?mt=2&uo=2.',
    'https://itunes.apple.com/us/podcast/dan-carlins-hardcore-history/id173001861?mt=2'
];
$podcast_ids = [];
$info = [];
foreach ($urls as $string) {
    if (preg_match('/id(\d+)/', $string, $match)) {
        $podcast_ids[] = $match[1];
    }
}
$json_array = json_decode(file_get_contents('https://itunes.apple.com/lookup?' . http_build_query(['id' => implode(',', $podcast_ids)])));
foreach ($json_array->results as $item) {
    $info[] = $item->artistName;
}
print '<pre>';
print_r($info);
print '</pre>';

EDIT 2

To put your object into an array just run it through this

foreach ($sxml->entries as $entry) {
    $urls[] = $entry->id[0];
}

When you access and object you use -> when you access an array you use []. Json and xml will parse out in to a combination of both objects and arrays. So you just need to follow the object's path and put the right keys in the right places to unlock that gate.

like image 66
Roger Avatar answered Jul 02 '26 03:07

Roger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!