Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php和Symfony2框架从URL读取数据的最佳方式是什么?_Php_Parsing_Url_Symfony - Fatal编程技术网

使用php和Symfony2框架从URL读取数据的最佳方式是什么?

使用php和Symfony2框架从URL读取数据的最佳方式是什么?,php,parsing,url,symfony,Php,Parsing,Url,Symfony,我正在使用symfony2框架构建一个项目。我必须解析用户输入的URL并对其进行分析。例如,如果用户输入一个类似“”的URL,程序应该提取型号名称、价格、图像等 是否有我可以使用的捆绑包或插件?您也可以使用文件获取内容获取数据,也可以使用Javascript获取数据,因为有许多可用选项: <?php $output = file_get_contents( 'http://www.website.com/'); $output = str_replace('

我正在使用symfony2框架构建一个项目。我必须解析用户输入的URL并对其进行分析。例如,如果用户输入一个类似“”的URL,程序应该提取型号名称、价格、图像等


是否有我可以使用的捆绑包或插件?

您也可以使用文件获取内容获取数据,也可以使用Javascript获取数据,因为有许多可用选项:

<?php
    $output = file_get_contents(
        'http://www.website.com/');
    $output = str_replace('rhs','"rhs"',$output);
    $output = str_replace('lhs','"lhs"',$output);
    $output = str_replace('error','"error"',$output);
    $output = str_replace('icc','"icc"',$output);

    $json = json_decode($output);
    $rhs = $json->rhs;
?>
和纯JavaScript脚本

function parseURLParams(url) {
  var queryStart = url.indexOf("?") + 1;
  var queryEnd   = url.indexOf("#") + 1 || url.length + 1;
  var query      = url.slice(queryStart, queryEnd - 1);

  if (query === url || query === "") return;

  var params  = {};
  var nvPairs = query.replace(/\+/g, " ").split("&");

  for (var i=0; i<nvPairs.length; i++) {
    var nv = nvPairs[i].split("=");
    var n  = decodeURIComponent(nv[0]);
    var v  = decodeURIComponent(nv[1]);
    if ( !(n in params) ) {
      params[n] = [];
    }
    params[n].push(nv.length === 2 ? v : null);
  }
  return params;
}
返回一个对象,如下所示:

{
  "a"  : ["a a"],     /* param values are always returned as arrays */
  "b b": ["b"],       /* param names can have special chars as well */
  "c"  : ["1", "2"]   /* an URL param can occur multiple times! */
  "d"  : [null]       /* params without values are set to null */ 
} 
所以

给予


严格说来,我还不知道该怎么做,但是在symfony代码中可以看到如何分析当前url并将其绑定到Request()对象中,同时提取所有GET和POST参数,这将是一件非常棒的事情

可以在服务/控制器内部分析URL,只需在$Request->query中使用GET params生成的请求对象即可


只是一个想法=)

但我需要的是URL内容……而不是参数……比如页面标题、产品价格(来自亚马逊页面)等等。。
var urlString = "http://www.foo.com/bar?a=a+a&b%20b=b&c=1&c=2&d#hash";
var urlParams = parseURLParams(urlString);
{
  "a"  : ["a a"],     /* param values are always returned as arrays */
  "b b": ["b"],       /* param names can have special chars as well */
  "c"  : ["1", "2"]   /* an URL param can occur multiple times! */
  "d"  : [null]       /* params without values are set to null */ 
} 
parseURLParams("www.mints.com?name=something")
{name: ["something"]}