如何解决PHP 脚本不发布数据
它应该如何工作:
- 用户在第 1 页上输入 URL。
- Page1 将 URL 发布到 page2。
- Page2 获取 URL 内容并将其发布回 page1。
- 第 1 页显示 URL 的内容。
发生了什么?
- Page2 没有回传任何内容。
第 1 页
<html>
<head>
<meta name="viewport" content="width=320,initial-scale=1">
<meta charset="utf-8">
<style>
body,html {
min-width: 100%;
min-height: 100%;
margin: 0;
padding: 0;
font: Arial 14px;
}
</style>
</head>
<body>
<form id="my-form"action="page2.php"method="post">
<input type="url" name="url" placeholder="Type or paste a valid URL" />
<input type="submit" value="Search"id="my-form-button">
<p id="my-form-status"></p>
</form>
<script>
window.addEventListener("DOMContentLoaded",function() {
// get the form elements defined in your form HTML above
var form = document.getElementById("my-form");
var button = document.getElementById("my-form-button");
var status = document.getElementById("my-form-status");
// Success and Error functions for after the form is submitted
function success() {
form.reset();
button.style = "display: none ";
status.innerHTML = "Success";
}
function error() {
status.innerHTML = "Oops! There was a problem.";
}
// handle the form submission event
form.addEventListener("submit",function(ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method,form.action,data,success,error);
});
});
// helper function for sending an AJAX request
function ajax(method,url,error) {
var xhr = new XMLHttpRequest();
xhr.open(method,url);
xhr.setRequestHeader("Accept","application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response,xhr.responseType);
} else {
error(xhr.status,xhr.response,xhr.responseType);
}
};
xhr.send(data);
}
</script>
<?php
if ($_POST)
echo $_POST['field1'];
?>
</body>
</html>
Page2
<?php
if($_POST)
$content=file_get_contents($_POST["url"]);
else
$content="No url specified.";
// where are we posting to?
$url = 'page1.php';
// what post fields?
$fields = array(
'field1' => $content,);
// build the urlencoded data
$postvars = http_build_query($fields);
// open connection
$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);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>
Page2 已发布到电子邮件 API 以进行测试,并且很少发送电子邮件。如果我让它发送 URL 而不是 URL 内容,它就可以正常工作。有人能弄清楚吗?事实上,我真的只是一个 PHP 或任何类型编码的初学者。
解决方法
@El_Vanja 感谢您的帮助。所以这个问题可以被认为是回答我现在发布你帮助我的代码以及我对缺少样式表问题的解决方案。我最终只是在输出结果之前将基本标签注入到结果中。
第 1 页
<body>
<div id="header" style="background-color:blue;
color:white;
text-align:center;
position:fixed;
width:100%;
">
<h1 id="title">
WEB PAGE DOWNLOADER
</h1>
<form id="my-form"action="page2.php"method="post">
<input type="url" inputmode="search" name="url" placeholder="Type or paste a valid URL" id="input"/>
<input type="submit" value="Search"id="my-form-button">
<p id="my-form-status"></p>
</form>
</div>
<script>
window.addEventListener("DOMContentLoaded",function() {
// get the form elements defined in your form HTML above
var form = document.getElementById("my-form");
var button = document.getElementById("my-form-button");
var status = document.getElementById("my-form-status");
var result = document.getElementById("myresult");
var header = document.getElementById("header");
var title = document.getElementById("title");
// Success and Error functions for after the form is submitted
function success(response,responseType,responseText) { status.innerHTML = "Url successfully posted to download script.";
document.write(responseText);
}
function error() {
status.innerHTML = "Oops! There was a problem.";
}
// handle the form submission event
form.addEventListener("submit",function(ev) {
ev.preventDefault();
var data = new FormData(form);
ajax(form.method,form.action,data,success,error);
});
});
// helper function for sending an AJAX request
function ajax(method,url,error) {
var xhr = new XMLHttpRequest();
xhr.open(method,url);
xhr.setRequestHeader("Accept","application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState !== XMLHttpRequest.DONE) return;
if (xhr.status === 200) {
success(xhr.response,xhr.responseType,xhr.responseText);
} else {
error(xhr.status,xhr.response,xhr.responseType);
}
};
xhr.send(data);
}
</script>
</body>
第 2 页
<?php
if($_POST){
$opts = array('http' =>
array(
'header' => 'User-agent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML,like Gecko) Version/3.0 Mobile/3B48b Safari/419.3',)
);
$context = stream_context_create($opts);
$content = file_get_contents($_POST["url"],false,$context);
// $content = file_get_contents($_POST["url"]);
if(!$content)
echo "ERROR: Download script received URL but could not return its contents.";
}
if(!$_POST){
echo "ERROR: Download script could not receive URL. Please use the <a href='page1.php'>WEB PAGE DOWNLOADER</a>. This is only a script." ;
}
if($content)
$repstr = "<head>
<base href='".$_POST["url"]."'>";
echo str_replace("<head>",$repstr,$content);
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。