我可以通过PHP发送带参数的URL并检索数据吗?

时间:2011-01-20 15:42:09

标签: php

我开始帮助一个运行网站的朋友,只需要一点点编码工作,所需的所有代码都是PHP。我是C#开发人员,所以这将是一个新的方向 我的第一个独立任务如下:
该网站被告知一种新的鱼类。例如,科学名称输入两个输入控件,一个用于属(X),另一个用于物种(Y)。这些名称需要以以下格式发送到网站:
http://www.fishbase.org/Summary/speciesSummary.php?genusname=X&speciesname=Y&lang=English
在结果页面上,还有常用名称和同义词的其他链接 我希望能够找到这些链接,并调用URL(因为这将包含获取特定数据所需的所有参数)并存储其中一些。
我希望保存两次调用的数据,一旦完成,将其全部转换为xml,然后可以上传到网站的数据库。
我想知道的是(a)可以做到这一点,(b)有多难? 提前致谢 马丁

5 个答案:

答案 0 :(得分:0)

我用了4年的史努比(http://sourceforge.net/projects/snoopy/)。 我从几个小时内发布它们的网站上获取了大约500个客户档案。

答案 1 :(得分:0)

a)是的 b)有经验时不难。

Google首先使用CURL,或者是allow_url_fopen。

答案 2 :(得分:0)

如果我理解正确,您希望脚本下载页面并处理下载的数据。如果是这样,答案是:

是的,是的 b)不难

:)

Oke ...这里有更多信息:我会使用CURL扩展,请参阅: http://php.net/manual/en/book.curl.php

<?php 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, "example.com"); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        $output = curl_exec($ch); 
        curl_close($ch);      
?>

答案 3 :(得分:0)

file_get_contents()将完成这项工作:

$data = file_get_contents('http://www.fishbase.org/Summary/speciesSummary.php?genusname=X&speciesname=Y&lang=English');

答案 4 :(得分:0)

// Отправить URL-адрес
function send_url($url, $type = false, $debug = false) { // $type = 'json' or 'xml'
    $result = '';
    if (function_exists('curl_init')) {
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
    } else {
        if (($content = @file_get_contents($url)) !== false) $result = $content;
    }
    if ($type == 'json') {
        $result = json_decode($result, true);
    } elseif ($type == 'xml') {
        if (($xml = @simplexml_load_file($result)) !== false) $result = $xml;
    }
    if ($debug) echo '<pre>' . print_r($result, true) . '</pre>';
    return $result;
}

$data = send_url('http://ip-api.com/json/212.76.17.140', 'json', true);