PHP-如何使用get或post方法抓取外部url并从中解析json数据?

PHP-如何使用get或post方法抓取外部url并从中解析json数据?,php,json,curl,zend-framework2,Php,Json,Curl,Zend Framework2,我有一个服务器使用ZF2运行,还有一个客户端使用PHP,现在我需要通过get或post方法从客户端读取json输出 如何从客户端抓取外部链接并解析JSON数据以读取'result'=>$msg的值 客户:: 你可以用 通过GET从服务器读取 您可以使用来解析结果您可以使用file\u get\u content来获取json数据。。但如果你也需要页面的内容 function crawl_page($url, $depth = 5) { $saved = array(); if

我有一个服务器使用ZF2运行,还有一个客户端使用PHP,现在我需要通过get或post方法从客户端读取json输出

如何从客户端抓取外部链接并解析JSON数据以读取'result'=>$msg的值

客户::

你可以用 通过GET从服务器读取


您可以使用来解析结果

您可以使用file\u get\u content来获取json数据。。但如果你也需要页面的内容

 function crawl_page($url, $depth = 5)
{
    $saved = array();
    if (isset($saved[$url]) || $depth === 0) {
        return;
    }

    $saved[$url] = true;

    $dom = new DOMDocument('1.0');
    @$dom->loadHTMLFile($url);

    $anchors = $dom->getElementsByTagName('a');
    foreach ($anchors as $element) {
        $href = $element->getAttribute('href');
        if (0 !== strpos($href, 'http')) {
            $path = '/' . ltrim($href, '/');
            if (extension_loaded('http')) {
                $href = http_build_url($url, array('path' => $path));
            } else {
                $parts = parse_url($url);
                $href = $parts['scheme'] . '://';
                if (isset($parts['user']) && isset($parts['pass'])) {
                    $href .= $parts['user'] . ':' . $parts['pass'] . '@';
                }
                $href .= $parts['host'];
                if (isset($parts['port'])) {
                    $href .= ':' . $parts['port'];
                }
                $href .= $path;
            }
        }
        $this->crawl_page($href, $depth - 1);
    }
    echo "URL:",$url,PHP_EOL,"CONTENT:",PHP_EOL,$dom->saveHTML(),PHP_EOL,PHP_EOL;
}

够快吗?处理数以百万计的请求
file-get-contents()
我的意思是?与什么相比足够快?和shell_exec相比,它的速度足够快,是吗?那就太好了。你可以同时尝试这两种方法和测量哪种方法更快。你必须查看
Zend Http Client
()
class AjaxController extends AbstractActionController {
    public function jsonAction(){
      $response = $this->getResponse();
      $msg = true;        
      $json = array(
        'result' => $msg,
        'module' => 'ajax',
        'data' => "test"
      );
      $response->setContent(Json::encode($json));
      return $response;            
      exit;
    }
}
 function crawl_page($url, $depth = 5)
{
    $saved = array();
    if (isset($saved[$url]) || $depth === 0) {
        return;
    }

    $saved[$url] = true;

    $dom = new DOMDocument('1.0');
    @$dom->loadHTMLFile($url);

    $anchors = $dom->getElementsByTagName('a');
    foreach ($anchors as $element) {
        $href = $element->getAttribute('href');
        if (0 !== strpos($href, 'http')) {
            $path = '/' . ltrim($href, '/');
            if (extension_loaded('http')) {
                $href = http_build_url($url, array('path' => $path));
            } else {
                $parts = parse_url($url);
                $href = $parts['scheme'] . '://';
                if (isset($parts['user']) && isset($parts['pass'])) {
                    $href .= $parts['user'] . ':' . $parts['pass'] . '@';
                }
                $href .= $parts['host'];
                if (isset($parts['port'])) {
                    $href .= ':' . $parts['port'];
                }
                $href .= $path;
            }
        }
        $this->crawl_page($href, $depth - 1);
    }
    echo "URL:",$url,PHP_EOL,"CONTENT:",PHP_EOL,$dom->saveHTML(),PHP_EOL,PHP_EOL;
}