【发布时间】:2016-02-09 03:14:04
【问题描述】:
我已搜索但无法将我的表单发布然后重定向到我自己的网址。我可以得到它或另一个,但不能两者兼而有之。如果我将 api 放入它写入数据库的表单操作中。 我试图允许用户提交表单,数据通过 API 传递到另一台服务器上的数据库,并且用户被重定向到我选择的页面。 以下问答是我最接近的 Using both form submission and redirection 在提交表单时,用户被发送到“sendthedata.php”,其中包含以下代码
现在,当我提交表单时,它会重定向到指定的页面,但不会发布任何数据。当我尝试使用javascript(来自示例链接)时,表单甚至没有提交,页面刷新并且数据在提交表单的url中。任何想法我做错了什么?我以前从未使用过 curl。
**更新了代码,但现在得到一个我不理解的输出页面
//create an array of data to be posted
$post_data['token'] = 'token';
$post_data['email'] = 'email';
$post_data['first_name'] = 'first_name';
$post_data['last_name'] = 'last_name';
$post_data['edu'] = 'edu';
$post_data['eduint'] = 'edu_int';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://DATABASE.com/listener.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
$post_string = http_build_query($post_data);
curl_setopt($curl_connection, CURLOPT_POST, true);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
header("location: mypage.php");
数组 ( [url] => http://DATABASE.com/listener.php [content_type] => 文本/html;字符集=UTF-8 [http_code] => 200 [header_size] => 387 [请求大小] => 292 [文件时间] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.156171 [namelookup_time] => 7.6E-5 [connect_time] => 0.060235 [pretransfer_time] => 0.060291 [size_upload] => 88 [size_download] => 27 [speed_download] => 172 [速度_上传] => 563 [下载内容长度] => 27 [上传内容长度] => 88 [开始传输时间] => 0.15614 [redirect_time] => 0 [certinfo] => 数组 () [primary_ip] => xxxxxxxxxxx [primary_port] => 80 [local_ip] => xxxxxxxxxxx [local_port] => 35623 [redirect_url] => ) 0- 警告:无法修改 标头信息 - 已发送的标头(输出开始于 /home/xxxxx/public_html/xxxxx/tst/poster.php:42) 在 /home/xxxxx/public_html/xxx/tst/poster.php 在第 52 行
【问题讨论】:
标签: forms api redirect post curl