Home >Backend Development >PHP Tutorial >PHP simulates form POST data_PHP tutorial
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 using PHP to simulate the POST action of the form. This requirement can be easily achieved using the CURL library.
The first step is to 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.'<br />'; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 设置是否显示header信息 0是不显示,1是显示 默认为0 //curl_setopt($curl, CURLOPT_HEADER, 0); // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。0显示在屏幕上,1不显示在屏幕上,默认为0 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 要验证的用户名密码 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']; // 微博id $post_data['title'][] = htmlspecialchars( $result['statuses'][$i]['user']['name'] . ':' . $this -> sysSubStr($result['statuses'][$i]['text'], 50, true) ); // 微博标题 $post_data['editor'][] = $result['statuses'][$i]['user']['name']; // 微博作者名 $post_data['userid'][] = $result['statuses'][$i]['user']['id']; // 微博作者id $post_data['logo'][] = $result['statuses'][$i]['user']['avatar_large']; // 微博作者头像 $post_data['part'][] = '官方微博'; // 微博出处 if(isset($result['statuses'][$i]['original_pic'])) { // 微博原图 $post_data['picture'][$i] = $result['statuses'][$i]['original_pic']; // 微博缩略图 $post_data['thumbnail'][$i] = $result['statuses'][$i]['thumbnail_pic']; $post_data['text'][] = $result['statuses'][$i]['text'].'<img src="'.$result['statuses'][$i]['thumbnail_pic'].'" />'; // 微博内容 } else { $post_data['text'][] = $result['statuses'][$i]['text']; // 微博内容 } }
Then encode the array into string data that matches the form POST, and then use the CURL library to POST it out.
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).'<br />'; $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); } } }
Another simple example:
$post_data = array(); $post_data['clientname'] = "test08"; $post_data['clientpasswd'] = "test08"; $post_data['submit'] = "submit"; $url='http://xxx.xxx.xxx.xx/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); //为了支持cookie curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $result = curl_exec($ch);