PHP interview on the topic (2018-3-3)

Source: Internet
Author: User

Requirements: Read all the first 3,000 data from the third-party API and deposit the corresponding database fields

Third party Api:http://pub.cloudmob.mobi/publisherapi/offers/?uid=92&key=d4bab08884781dbf2bede528e27d243d&limit =1000&page=1

SQL code:

/*Navicat MySQL Data transfersource server:testsource Server version:50714source host:localhost: 3306Source database:testtarget Server type:mysqltarget server Version:50714file encoding:65001dat e:2018-03-04 10:02:51*/SETForeign_key_checks=0;-- ------------------------------Table structure for offers-- ----------------------------DROP TABLE IF EXISTS' offers ';CREATE TABLE' offers ' (' ID ')int(Ten) unsigned not NULLauto_increment, ' description 'text  not NULL, ' Country 'varchar(255) not NULL, ' allowedtraffic 'varchar(255) not NULL, ' oid 'varchar(Ten) not NULL, ' avail 'varchar(255) not NULL, ' name 'varchar(255) not NULL, ' link 'text  not NULL, ' preview 'text  not NULL,  PRIMARY KEY(' id ')) ENGINE=MyISAM auto_increment=25575 DEFAULTCHARSET=UTF8;

PHP Code:

<?PHP//idea://1. First remove the data from PHP's curl and convert it to object//2. Connect to the database, do preprocessing//3. Converts the value taken to an array and binds the database parameter//4. Execute SQL operation, data fetch succeeds// PS: The initialization of data and the closing of a URL session can only be performed once, so it is placed behind the for loop    $curl=Curl_init (); $page=1;  for($j= 0;$j<3;$j++){        $url= "Http://pub.cloudmob.mobi/publisherapi/offers/?uid=92&key=d4bab08884781dbf2bede528e27d243d&limit=1 &page=$page"; //echo $url;curl_setopt ($curl, Curlopt_url,$url ); curl_setopt ($curl, Curlopt_header, 0); curl_setopt ($curl, Curlopt_returntransfer, 1); $data= Curl_exec ($curl); //To convert a JSON string to an object         $data=json_decode ($data); $con=Mysqli_connect("localhost", "root", "" "," Test ")); if(!$con-connect_error) {             Echo $con-Connect_error; } mysqli_set_charset ($con, "gb2312"); $sql= "INSERT into offers (Description,country,allowedtraffic,oid,avail,name,link,preview) VALUES (?,?,?,?,?,?,?,?);"; $sql _stmt=$con->prepare ($sql) or die($con-error);  for($i= 0;$i<1;$i++) {             //to convert the value of an object to an array            $oid=json_encode ($data->data[$i]->OID); $avail=json_encode ($data->data[$i]->avail); $name=json_encode ($data->data[$i]->name); $link=json_encode ($data->data[$i]->link); $preview=json_encode ($data->data[$i]->preview); $allowedtraffic=json_encode ($data->data[$i]->allowedtraffic); $description=json_encode ($data->data[$i]->description); $country=json_encode ($data->data[$i]->country); $sql _stmt->bind_param (' Ssssssss ',$description,$country,$allowedtraffic,$oid,$avail,$name,$link,$preview); $b=$sql _stmt-execute (); }         if(!$b) {             Echo"Operation failed."$sql _stmt-error; }Else{             Echo"Operation succeeded"; }         //Release            $page=$page+1; $con-Close (); } curl_close ($curl);?>

PHP interview on the topic (2018-3-3)

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.