【发布时间】:2014-06-12 22:07:59
【问题描述】:
我正在尝试将表单中的$_POST 输入插入到 php 页面中。有什么理由不应该这样做吗?我没有收到任何错误,但也没有得到预期的结果
HTML
<form action="cross-domain-page.php" method="post">
<input type="text" name="phone" value="555555555">
<input type="text" name="fname" value="john">
<input type="text" name="lname" value="doe">
<input type="text" name="email" value="example@address.com">
<input type="text" name="attr" value="<xml>xml-value</xml>">
<input type="submit" name="submit" value="submit">
</form>
PHP
<?php
/*
$phone = $_POST['phone'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$attr = $_POST['attr'];
*/
/**
* Define POST URL and also payload
*/
define('XML_PAYLOAD', '<subscriptions><opt_in>invite</opt_in><user><mobile-phone>' . $_POST['phone'] . '</mobile-phone><first-name>' . $_POST['fname'] . '</first-name><last-name>' . $_POST['lname'] . '</last-name><email>' . $_POST['email'] . '</email>' . $_POST['attr'] . '</user></subscriptions>');
define('XML_POST_URL', $_POST['URL']);
/**
* Initialize handle and set options
*/
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_URL, XML_POST_URL);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_POSTFIELDS, XML_PAYLOAD);
curl_setopt($ch, CURLOPT_VERBOSE, true);
/**
* Execute the request and also time the transaction
*/
$start = array_sum(explode(' ', microtime()));
$result = curl_exec($ch);
$stop = array_sum(explode(' ', microtime()));
$totalTime = $stop - $start;
/**
* Check for errors
*/
if ( curl_errno($ch) ) {
$result = 'cURL ERROR -> ' . curl_errno($ch) . ': ' . curl_error($ch);
} else {
$returnCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
switch($returnCode){
case 200:
break;
default:
$result = 'HTTP ERROR -> ' . $returnCode;
break;
}
}
/**
* Close the handle
*/
curl_close($ch);
/**
* Output the results and time
*/
echo 'Total time for request: ' . $totalTime . "\n";
echo $result;
/**
* Exit the script
*/
exit(0);
?>
【问题讨论】:
-
define('XML_POST_URL', $_POST['URL']); -
除此之外没有什么问题吗?
-
我不这么认为。你有错误吗?
-
@JosephCasey:这是一个真正的问题吗?如果是这样,请询问它,因为到目前为止这很难破译。并且还尝试创建一些引发相同问题的不同示例代码。我认为您足够聪明,可以尝试一个与众不同的示例(提示:您重复使用的原始代码确实很糟糕,您有足够的智慧编写自己的代码。当您了解更多时,无需接管其他错误)。
-
并且不要对变量使用常量。那不是因为它们有好处(即使你只读过一次)
标签: php xml post http-post http-post-vars