执行;javascript/jQuery类似于;使用PHP的函数

执行;javascript/jQuery类似于;使用PHP的函数,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我正在尝试将一些处理从客户端移动到服务器端 我是通过AJAX实现的 在本例中,t是如下所示的URL: 第一个问题,我需要通过这个小函数发送一组URL,以使用我的示例拉出“1081244497”。下面用javascript实现了这一点,但不确定如何在PHP中使其循环 var e = t.match(/id(\d+)/); if (e) { podcastid= e[1]; } else { podcastid = t.m

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

我是通过AJAX实现的

在本例中,
t
是如下所示的URL:

第一个问题,我需要通过这个小函数发送一组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
s列表,而不一次传递一个(但这可能是唯一的方法)

关于如何在这里开始的想法

主要编辑

好的…让我澄清一下我现在需要什么,给出一些评论

我用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),然后通过下面这样的函数运行它们,结果是:“9179185701081244497”,它只是URL的一部分,由逗号连接

我有一个功能,可以一次获取一个身份证号码,但正在努力解决
foreach
的工作方式(另外,我知道必须有更好的方法来实现此功能):


查找match
preg\u match()
http\u build\u query()
将数组转换为查询字符串。和
file\u get\u contents()
用于请求数据。和
json\u 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\u r()
var\u dump()
进行调试

苹果API使用
来分离ID

您将得到返回到数组中的多个结果,您可以使用
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>';

当您访问和对象时,您使用
->
当您访问阵列时,您使用
[]
。Json和xml将解析为对象和数组的组合。所以你只需要沿着物体的路径,把正确的钥匙放在正确的地方就可以打开那扇门。

@MarcB我需要一段时间才能通过,但我会的。关于提取ID的第一部分呢?你的问题基本上是:(1)如何在PHP中使用正则表达式?(2) 如何在PHP中发出HTTP请求?(3) iTunesAPI有批量查询查找吗?我猜是解析url吗?@David嗯,这比我的要简洁得多,但我认为基本上就是这样。我确实知道regex,但不太确定如何循环命中我所有的URL并将它们存储在一个变量中。@jonmrich:你试过什么?如果您有一个URL数组,那么您不会将解析的标识符存储在一个字符串数组中吗?你是在问数组是如何工作的吗?循环是如何工作的?现在还不清楚你到底被困在了哪里。这真的很有帮助,尤其是苹果API的语法。我刚刚对我的问题做了一个重大更新,你能再看一眼吗?@jonmrich我编辑了我的答案,这就是你获得艺术家名字的方式。这个编辑非常完美。挑战在于,我正在动态地拉取URL(在同一个PHP文件中),其中可能有多达100个URL。有没有一种方法可以动态创建一个对象,我可以将它传递到这个对象中,而不需要生成100行
$ulrs[]
行(我甚至不确定是否可以完成)?它所需要的就是一个数组。您是如何存储数据的?只需执行foreach循环并将它们添加到数组中。在演示itIf的过程中,请稍等片刻,$sxml=simplexml\u load\u文件($url)变量转储($sxml->entry->id)这给了我:
对象(simplexmlement)#307(1){[0]=>string(64)”https://itunes.apple.com/us/podcast/serial/id917918570?mt=2&uo=2“}
这是整个100个列表中的第一个id,所以这是我需要迭代的内容。
$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>';
foreach ($sxml->entries as $entry) {
    $urls[] = $entry->id[0];
}