先说说读取的函数fopen()打开URL获得网页内容,使用fgets()这个函数来获取字符串。fgets()函数是从文件指针中读取一行。文件指针必须是有效的,必须指向由 fopen() 或 fsockopen() 成功打开的文件(并还未由 fclose() 关闭)。最后使用fclose()函数关闭。
代码:
<?php
/**
* Created by PhpStorm.
* User: liang
* Date: 2017/6/19
* Time: 15:59
*/
require_once(dirname(__FILE__)."/config.php");
include DedeInclude('templets/sale_yijian.htm');
//增加权限检查
if(empty($dopost)) $dopost = "";
$get = fopen('http://www.***?type=productType', 'r');
if($get) {
while(!feof($get)){
$oneline = fgets($get);
$contents = explode("**",$oneline);
$id = $contents[0];
$name = $contents[1];
$er = $contents[2];
$err = $contents[3];
if($name==null||$err!=null){
echo "本条数据格式不正确,上传失败".'<br/>';
}else{
echo $id."|".$name.'<br/>';
$inquery = "INSERT INTO `#@__products_type`(id,name) VALUES ('$id','$name')";
$dsql->ExecuteNoneQuery($inquery);
}
}
}else{
echo "更新产品类型失败!请使用“上传产品资料”功能更新数据!".'</br>';die;
}
$fh = fopen('http://www.***?type=product', 'r');
if($fh){
echo "内容为:".'<br/>';
while(!feof($fh)){
$oneline = fgets($fh);
$contents = explode("**",$oneline);
$id = $contents[0];
$progress = $contents[1];
$testDepartmentID = $contents[2];
$ProductName = $contents[3];
$ProductModel = $contents[4];
$ProductType = $contents[5];
$ProductTypeName = $contents[6];
$SendCompany = $contents[7];
$SendCompanyAddress = $contents[8];
$SendCompanyZIP = $contents[9];
$SendCompanyPhone = $contents[10];
$SendCompanyFax = $contents[11];
$SendCompanyAtificialPerson = $contents[12];
$ReportNo = $contents[13];
$ReportResult = $contents[14];
$XKZNo = $contents[15];
$XKZValidDate_Begin = $contents[16];
$XKZValidDate_End = $contents[17];
$deleted = $contents[18];
$er = $contents[19];
$err = $contents[20];
if($deleted==null||$err!=null){
echo "本条数据格式不正确,上传失败".'<br/>';
}else{
$row =$dsql->GetOne("SELECT id FROM `#@__products` WHERE ReportNo = '$ReportNo'");
$shuzu = $row['id'];
echo $id ."|". $progress ."|". $testDepartmentID ."|". $ProductName ."|". $ProductModel ."|". $ProductType ."|". $ProductTypeName ."|". $SendCompany ."|". $SendCompanyAddress ."|". $SendCompanyZIP ."|". $SendCompanyPhone ."|". $SendCompanyFax ."|". $SendCompanyAtificialPerson ."|". $ReportNo ."|". $ReportResult ."|". $XKZNo ."|". $XKZValidDate_Begin ."|". $XKZValidDate_End ."|". $deleted . '<br/>' ;
if(!empty($shuzu)){
$update = "update `#@__products` SET id='$id',progress='$progress',
testDepartmentID='$testDepartmentID',
ProductTypeName = '$ProductTypeName',
SendCompany='$SendCompany',
SendCompanyAddress = '$SendCompanyAddress',
SendCompanyZIP='$SendCompanyZIP',
SendCompanyPhone = '$SendCompanyPhone',
SendCompanyFax='$SendCompanyFax',
SendCompanyAtificialPerson = '$SendCompanyAtificialPerson',
ReportNo='$ReportNo',
ReportResult = '$ReportResult',
XKZNo='$XKZNo',
XKZValidDate_Begin = '$XKZValidDate_Begin',
XKZValidDate_End='$XKZValidDate_End',
deleted = '$deleted'
WHERE ReportNo = '$ReportNo' ";
$dsql->ExecuteNoneQuery($update);
}
else{
$inquery = "INSERT INTO `#@__products`(id,progress,testDepartmentID,ProductName,ProductModel,ProductType,ProductTypeName,SendCompany,SendCompanyAddress,SendCompanyZIP,SendCompanyPhone,SendCompanyFax,SendCompanyAtificialPerson,ReportNo,ReportResult,XKZNo,XKZValidDate_Begin,XKZValidDate_End,deleted)
VALUES ('$id','$progress','$testDepartmentID','$ProductName','$ProductModel','$ProductType','$ProductTypeName','$SendCompany','$SendCompanyAddress','$SendCompanyZIP','$SendCompanyPhone','$SendCompanyFax','$SendCompanyAtificialPerson','$ReportNo','$ReportResult','$XKZNo','$XKZValidDate_Begin','$XKZValidDate_End','$deleted')";
$dsql->ExecuteNoneQuery($inquery);
}
}
}
}else{echo "更新产品数据失败!请使用“上传产品资料”功能更新数据!";}
fclose($get);
fclose($fh);
根据url内容的固定格式(url是固定好格式的文本文档),进行分割字符串。然后存储到数据库中。通过后台系统就可以实现一键更新数据的功能。