Home > php教程 > php手册 > PHP模拟表单POST数据

PHP模拟表单POST数据

WBOY
Release: 2016-06-13 09:38:59
Original
1075 people have browsed it

新浪微博提供的API为JSON格式,我们写一个PHP脚本将其转化成数组并且像表单一样发布到我们的网站。这就需要使用PHP去模拟表单的POST动作,使用CURL库可以很方便地实现这个需求。

首先是将JSON转化成数组。

		$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'].'" / alt="PHP模拟表单POST数据" >';	// 微博内容
			}
			else
			{
				$post_data['text'][] = $result['statuses'][$i]['text'];	// 微博内容
			}
		}
Copy after login

然后将数组经过URL编码编程符合表单POST的字符串数据,再使用CURL库将其POST出去。

		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);
				}
			}
		}
Copy after login

再来一个简单的例子:

$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);
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template