PHP fopen读取url内容

先说说读取的函数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是固定好格式的文本文档),进行分割字符串。然后存储到数据库中。通过后台系统就可以实现一键更新数据的功能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值