使用 PHP 执行“javascript/jQuery-like”函数


Perform "javascript/jQuery-like" functions using PHP

我正在尝试将一些处理从客户端移动到服务器端。

我是通过 AJAX 执行此操作的。

在本例中t是如下所示的 URL:https://itunes.apple.com/us/podcast/real-crime-profile/id1081244497?mt=2&uo=2。

第一个问题,我需要通过这个小函数发送一堆这样的 URL,以使用我的示例拉出"1081244497"。下面在javascript中完成此操作,但不确定如何在PHP中使其循环。

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

下一部分比较棘手。我可以一次将这些podcastid之一传递到 AJAX 中,并取回我需要的内容,如下所示:

 $.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); 
          },  
      });

我不知道该怎么做的是在 PHP 中完成同样的事情,但也要使用podcastid列表而不一次传递一个(但这可能是唯一的方法(。

关于如何从这里开始的想法?

主要编辑

好。。。鉴于一些评论,让我澄清我现在需要什么。

我在 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);

这给了我:

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"
      }
    }
  }
}

我现在需要的是提取每个字符串(URL(,然后通过如下所示的函数运行它们,最终得到:"917918570,1081244497",这只是 URL 的一部分,由逗号连接。

我有这个函数一次获取一个的 id 号,但苦苦挣扎于foreach如何工作(另外我知道必须有更好的方法来执行此功能(:

$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

查找匹配preg_match()http_build_query()将数组转换为查询字符串。并file_get_contents()用于数据请求。并json_decode()将 JSON 响应解析为 PHP 数组。

最后它应该看起来像这样。

$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];
}

你可能不得不搞砸一下。不过,这应该会让你走上正确的轨道。如果遇到问题,可以随时使用print_r()var_dump()进行调试。

就Apple API而言,使用,来分隔ID

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

您将获得多个返回到数组中的结果,您可以使用 foreach() 循环来解析它们。

编辑

这是一个完整的示例,它从 url 列表中获取艺术家姓名

$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>';

编辑 2

要将对象放入数组中,只需通过以下命令运行它

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

当您访问和对象时,您使用->当您访问数组时,您使用[] .Json 和 xml 将解析为对象和数组的组合。因此,您只需要遵循对象的路径并将正确的钥匙放在正确的位置即可解锁该门。