如何在php中向网页中的表单发出HTTP请求
我努力实现的目标是: 我有一个网站,我有完整的源代码访问。本网站中的页面是使用velocity模板创建的,我有一个页面,其格式如下如何在php中向网页中的表单发出HTTP请求,php,forms,http,curl,velocity,Php,Forms,Http,Curl,Velocity,我努力实现的目标是: 我有一个网站,我有完整的源代码访问。本网站中的页面是使用velocity模板创建的,我有一个页面,其格式如下 <h3>form data</h3> <form action="$portalPath/test" method="post"> <input type="text" name="text" value="$!self.getTextFromFormData()" /> <input type=
<h3>form data</h3>
<form action="$portalPath/test" method="post">
<input type="text" name="text" value="$!self.getTextFromFormData()" />
<input type="submit" />
</form>
但结果显示了我访问的模板(即测试)的html源,而不是我要下载的html文件。我想做的是发出一个http请求,自动在表单中输入文件名,并使表单自动提交请求并进行处理,从而下载所需的html文件。我不知道这是否可能,或者这是否是正确的方法。如果这可以用curl来实现,那就更好了。任何想法都将受到高度赞赏 见:
因此,从引用的URL:
<?php
$url = 'http://localhost/portal/default/test';
$fields = array(
'text'=>urlencode($value_for_field_text),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
// Initialize curl
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
// Results of post in $result
?>
$portalPath/test
是否总是生成相同的URL?是的。在这之后是python脚本,它处理提交的数据,可能是
<?php
$url = 'http://localhost/portal/default/test';
$fields = array(
'text'=>urlencode($value_for_field_text),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
// Initialize curl
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
// Results of post in $result
?>