【发布时间】:2016-01-19 01:34:03
【问题描述】:
我有一个简单的表单,我想将它转换成 PHP 后端系统。
现在这个表单有一个提交到 URL 的操作 - 只有当名称为 postdata 的数据和正确的信息被提交(xml 已被编码)时,该 URL 才接受邀请。
工作原理: - 请注意,输入名称称为postdata,值包含xml,它一直是urlencoded,并且工作正常。
<form name="nonjava" method="post" action="https://url.com">
<input name="postdata" id="nonjavapostdata" type="hidden" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;" />
<button type="submit" name="submit">Button</button>
</form>
但是,我想通过在 PHP 中移动 postdata 元素来实现以下目标,因为很多信息都是通过这种方式传递的。
请注意:链接是 https,但它在本地不起作用,所以我不得不在 CURL 中禁用它。
<?php
$url = 'https://super.com';
$xmlData = '<?xml version="1.0" encoding="utf-8"?>
<postdata
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<api>2</api>
</postdata>';
$testing = urlencode($xmlData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "postdata=" . $testing);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
echo $header_size . '<br>';
echo htmlspecialchars($header) . '<br>';
echo htmlspecialchars($body) . '<br><br>';
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "$http_status for $url <br>";
?>
我不断收到 302 错误(这是 url 内容未找到数据,因此重定向到未找到的页面。)
【问题讨论】:
-
我认为您正在寻找 CURLOPT_FOLLOWLOCATION 卷曲标志