[英]PHP - how to crawl external url using get or post method and parse json data from it?
I have one server running with ZF2 and i have another client with PHP, now from client i need to read the json output via get or post method. 我有一个运行ZF2的服务器,我有另一个PHP客户端,现在从客户端我需要通过get或post方法读取json输出。
How to crawl external link form client and parse the JSON data to read the value of 'result' => $msg? 如何抓取外部链接表单客户端并解析JSON数据以读取'result'=> $ msg的值?
Client: http://www.other-end-user.com/view : 客户: http : //www.other-end-user.com/view :
<?php
$customerid = $_GET['id'];
$id = substr('3' . rand(0,9999), 0-$length);
$default = "https://toogle.com?id={$id}";
$b = "<a href={$default} id={$customerid}button class={$customerid}button><img src=\"http://toogle.com/button.png\" /></a>";
#curl "http://www.external-analytics-site.com/ajax/json" | grep result
$live = true
if ($live){
echo $b;
} else {
echo "Live query failed";
}
?>
Server (ZF2): http://www.external-analytics-site.com/ajax/json : 服务器(ZF2): http : //www.external-analytics-site.com/ajax/json :
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;
}
}
you can use file-get-contents() to read from the server via GET 你可以使用file-get-contents()通过GET从服务器读取
you can use json_decode to parse the result 你可以使用json_decode来解析结果
You can use file_get_content to get json data .. but If you need content of page too.. 您可以使用file_get_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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.