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