zoukankan      html  css  js  c++  java
  • 远程读取json数据并写入数据库

    参考:http://www.jb51.net/article/39937.htm

    $curlPost = 'a=1&b=2';//模拟POST数据
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:0.0.0.0', 'CLIENT-IP:0.0.0.0'));  //构造IP
    curl_setopt($ch, CURLOPT_REFERER, "http://www.jb51.net/");   //构造来路 
    curl_setopt($ch,CURLOPT_URL, 'http://www.jb51.net');//需要抓取的页面路径
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);//post值

    $file_contents = curl_exec($ch);//抓取的内容放在变量中
    curl_close($ch)

    <?php
    $mysql_server_name='localhost';
    $mysql_username='test';
    $mysql_password='test';
    $mysql_database='apitest';
    $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ;
    mysql_select_db($mysql_database);

    $a=json_decode(request_post($url="http://weshop.zocai.com/Api/Global/mod_address_linkage_check/",$param="parentid"),true);
    //var_dump($a);

    foreach($a['info'] as $row) {
    //print_r($row);
    $statement = "INSERT INTO `apitest` (id, status, areaid, parentid, name, remark, create_time, sort, level) VALUES ";
    $statement .= ' ("' . implode($row, '","') . '")';
    // echo $statement;
    mysql_query($statement,$conn);
    echo mysql_error();
    // exit;

    }

    function request_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
    return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    $ch = curl_init();//初始化curl
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
    }


    ?>

  • 相关阅读:
    解决UITableView中Cell重用机制导致内容出错的方法总结
    Hdu 1052 Tian Ji -- The Horse Racing
    Hdu 1009 FatMouse' Trade
    hdu 2037 今年暑假不AC
    hdu 1559 最大子矩阵
    hdu 1004 Let the Balloon Rise
    Hdu 1214 圆桌会议
    Hdu 1081 To The Max
    Hdu 2845 Beans
    Hdu 2955 Robberies 0/1背包
  • 原文地址:https://www.cnblogs.com/gavinyyb/p/6214431.html
Copyright © 2011-2022 走看看