这是一个编辑过的代码,其中先生。 Paulo Rodrigues,php curl用于获取指定网页中的条目或数据。它将保存在file.txt中。打开文件然后获取每个条目的数据然后循环,以便插入条目中所需的所有数据。
$ch = curl_init("http://www.uniprot.org/uniprot/?query=(annotation%3a(type%3asignal+confidence%3aexperimental))+AND+reviewed%3ayes&sort=score&limit=10&format=txt");
$fp = fopen("file.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
while (!feof($fp))
{
$array = array();
$index = -1;
foreach ($fp as $line) {
if (preg_match('#^ID#', $line)) {
$index++;
}
$array[$index][] = $line;
}
foreach ($array as $block) {
$fields = array();
foreach ($block as $item) {
if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
print_r($fields['prot_name'] = $matches[1]);
echo "      ";
}
else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
print_r($fields['tax_lin'] = $matches[1]);
echo "         
           ";
}
else if (preg_match('#LOCATION:\sMembrane;\s(.*?)\s#', $item, $matches)) {
print_r($fields['sub_loc'] = $matches[1]);
}
}
echo "<br />";
if (!empty($fields)) {
$f = implode(', ', array_keys($fields));
$v = "'" . implode("', '", $fields) . "'";
$sql = "INSERT INTO swissprot ($f) VALUES($v)";
mysql_query($sql) or die(mysql_error());
}
}
}
curl_close($ch);
fclose($fp);
问题是有一个警告显示:“警告:此行中为foreach()提供的参数无效。可能是什么问题?
foreach ($fp as $line) {
if (preg_match('#^ID#', $line)) {
$index++;
}
$array[$index][] = $line;
}
答案 0 :(得分:0)
你可以试试这个:
$webpagedata = file('http://www.uniprot.org/uniprot/?query=annotation%3a%28type%3asignal+confidence%3aexperimental%29+reviewed%3ayes&sort=score&limit=10&format=txt');
$array = array();
$index = -1;
foreach ($webpagedata as $line) {
if (preg_match('#^ID#', $line)) {
$index++;
}
$array[$index][] = $line;
}
foreach ($array as $block) {
$fields = array();
foreach ($block as $item) {
if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
$fields['prot_name'] = $matches[1];
} else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
$fields['tax_lin'] = $matches[1];
} else if (preg_match('#LOCATION:\sMembrane;\s(Single-pass)#', $item, $matches)) {
$fields['sub_loc'] = $matches[1];
}
}
if (!empty($fields)) {
$f = implode(', ', array_keys($fields));
$v = "'" . implode("', '", $fields) . "'";
$sql = "INSERT INTO spdb ($f) VALUES($v)";
mysql_query($sql) or die(mysql_error());
}
}
<强>更新强>
$ch = curl_init("http://www.uniprot.org/uniprot/?query=(annotation%3a(type%3asignal+confidence%3aexperimental))+AND+reviewed%3ayes&sort=score&limit=10&format=txt");
$fp = fopen("file.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
$file = file("file.txt");
$array = array();
$index = -1;
foreach ($file as $line) {
if (preg_match('#^ID#', $line)) {
$index++;
}
$array[$index][] = $line;
}
foreach ($array as $block) {
$fields = array();
foreach ($block as $item) {
if (preg_match('#^ID\s+(.*?)\s#', $item, $matches)) {
print_r($fields['prot_name'] = $matches[1]);
echo "      ";
} else if (preg_match('#^OC\s+(Eukaryota)#', $item, $matches)) {
print_r($fields['tax_lin'] = $matches[1]);
echo "         
           ";
} else if (preg_match('#LOCATION:\sMembrane;\s(.*?)\s#', $item, $matches)) {
print_r($fields['sub_loc'] = $matches[1]);
}
}
echo "<br />";
if (!empty($fields)) {
$f = implode(', ', array_keys($fields));
$v = "'" . implode("', '", $fields) . "'";
$sql = "INSERT INTO swissprot ($f) VALUES($v)";
mysql_query($sql) or die(mysql_error());
}
}