PHP simulates form POST data. Sina Weibo provides APIs in JSON format. we write a PHP script to convert the API into an array and publish it to our website like a form. This requires PHP to simulate the form POST action. The API provided by Sina Weibo is in JSON format. we write a PHP script to convert it into an array and publish it to our website like a form. This requires PHP to simulate the form POST action, which can be easily implemented using the CURL Library.
First, convert JSON into an array.
$ Count = 15; $ url = "https://api.weibo.com/2/statuses/home_timeline.json? Source = bkjia & count = ". $ count." & page = 1 "; echo $ url .'
'; $ Curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); // you can specify whether to display header information. 0 indicates that header information is not displayed, 1 indicates that the default value is 0 // curl_setopt ($ curl, CURLOPT_HEADER, 0); // sets the cURL parameter and requires that the result be saved to the string or output to the screen. 0 is displayed on the screen, and 1 is not displayed on the screen. the default value is 0curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // the username and password to verify curl_setopt ($ curl, CURLOPT_USERPWD, "zhuhaixinwenwang@sina.cn: 2639996"); $ data = curl_exec ($ curl); curl_close ($ curl ); $ result = json_decode ($ data, true); $ post_data = array (); for ($ I = 0; $ I <$ count; $ I ++) {$ post_data ['mid '] [] = $ result ['statuses'] [$ I] ['mid ']; // Weibo id $ post_data ['title'] [] = htmlspecialchars ($ result ['statuses '] [$ I] ['user'] ['name']. ':'. $ this-> sysSubStr ($ result ['statuses '] [$ I] ['text'], 50, true )); // microblog Title $ post_data ['editor'] [] = $ result ['statuses '] [$ I] ['user'] ['name']; // Weibo author name $ post_data ['userid'] [] = $ result ['statuses '] [$ I] ['user'] ['id']; // Weibo author id $ post_data ['logo '] [] = $ result ['statuses'] [$ I] ['user'] ['Avatar _ large ']; // microblog author profile picture $ post_data ['part'] [] = 'official microblog '; // Weibo source if (isset ($ result ['statuses '] [$ I] ['original _ pic']) {// Weibo Source image $ post_data ['picture '] [$ I] = $ result ['statuses'] [$ I] ['original _ pic ']; // Weibo thumbnail $ post_data ['thumbnail '] [$ I] = $ result ['statuses'] [$ I] ['thumbnail _ pic ']; $ post_data ['text'] [] = $ result ['statuses '] [$ I] ['text']. ''; // Weibo content} else {$ post_data ['text'] [] = $ result ['statuses '] [$ I] ['text']; // Weibo content }}
Then, the array is programmed by URL encoding to conform to the string data of form POST, and then POST it using the CURL Library.
For ($ j = 0; $ j <$ count; $ j ++) {$ o = ""; foreach ($ post_data as $ k => $ v) {$ o. = "$ k = ". urlencode ($ post_data [$ k] [$ j]). "&" ;}$ post_str = substr ($ o, 0,-1); if (file_get_contents ($ mid_file) ==$ post_data ['mid '] [$ j]) {break;} else {echo urldecode ($ post_str ).'
'; $ Ch = curl_init (); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_URL, 'http: // x.zhnews.net/post.data.php? Db = weibosina '); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_str); $ result = curl_exec ($ ch); if (curl_errno ($ ch )) {// print curl_error ($ ch );}}}
Here is a simple example:
$ Post_data = array (); $ post_data ['clientname'] = "test08"; $ post_data ['clientpasswd'] = "test08 "; $ post_data ['submit '] = "submit"; $ url = 'http: // xxx. xxx. xxx. xx/xxx/top. php '; $ o = ""; foreach ($ post_data as $ k => $ v) {$ o. = "$ k = ". urlencode ($ v ). "&" ;}$ post_data = substr ($ o, 0,-1); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_POST, 1 ); curl_setopt ($ ch, CURLOPT_HEADER, 0); curl_setopt ($ ch, CURLOPT_URL, $ url); // to support cookiecurl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt '); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); $ result = curl_exec ($ ch );
Bytes. This requires PHP to simulate the form POST action...