【问题标题】:Form submission to api and then redirect to a specifi page表单提交到api,然后重定向到特定页面
【发布时间】: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


    【解决方案1】:

    从您的代码中,我可以看到您已经用 curl 注释掉了发布的数据。启用它。

    另外,请使用http_build_query() 准备您的帖子数据,而不是手动准备它,以避免任何与特殊字符有关的问题。所以总的来说它看起来像下面这样:

    $post_string = http_build_query($post_data);
    curl_setopt($curl_connection, CURLOPT_POST, true);
    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
    

    【讨论】:

    • 我没有注释掉代码,它所做的只是显示在下一页上,不再允许重定向。它仍然不会通过 api 将数据发布到数据库。
    • 不是取消注释,使用上面的代码,如果它不起作用,然后用代码更新您的问题,以便其他人可以帮助您。
    • 从您的错误消息来看,还有一点……您在header("...") 调用之前正在执行print_r。这是有问题的。在 HTTP 中,应在任何输出之前先打印标头。您可以使用ob_start() 函数环顾四周。可以在以下位置找到有关此类似问题的详细信息:stackoverflow.com/questions/8028957/…
    • 很好的链接。这确实修复了标题错误,并且当我将标题重定向放在“print_r”卷曲代码之前时它会重定向。但没有任何东西被发布到数据库中。查看 curl 代码以尝试弄清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2022-09-26
    • 1970-01-01
    • 2017-06-26
    相关资源
    最近更新 更多