Read JSON data remotely and write to the database

Source: Internet
Author: User

Reference: http://www.jb51.net/article/39937.htm

$curlPost = ' a=1&b=2 ';//Analog post data
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_httpheader, Array (' x-forwarded-for:0.0.0.0 ', ' client-ip:0.0.0.0 ')); Constructing IP
curl_setopt ($ch, Curlopt_referer, "http://www.jb51.net/"); Construction route
curl_setopt ($ch, Curlopt_url, ' http://www.jb51.net ');//page path to crawl
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_timeout, 30);
curl_setopt ($ch, Curlopt_postfields, $curlPost);//post value

$file _contents = curl_exec ($ch);//The content of the grab is placed in a variable
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 ();//Initialize Curl
curl_setopt ($ch, Curlopt_url, $POSTURL);//crawl specified Web page
curl_setopt ($ch, Curlopt_header, 0);//Set HEADER
curl_setopt ($ch, Curlopt_returntransfer, 1);//requires the result to be a string and output to the screen
curl_setopt ($ch, Curlopt_post, 1);//post Submission method
curl_setopt ($ch, Curlopt_postfields, $curlPost);
$data = curl_exec ($ch);//Run Curl
Curl_close ($ch);
return $data;
}


?>

Read JSON data remotely and write to the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.