2010-08-23 155 views
2

当我开发一个正在开发的应用程序时,我遇到了一个有趣的问题。希望有人知道为什么会发生这种情况/如何调整我的工作流程!可以通过浏览器查看URL,但不能通过CURL查看URL

背景:
我正在写一个应用程序,帮助大学的学生彼此联系。其基本工作流程如下:

  1. 为服务用户注册
  2. 该应用程序使用卷曲轮询大学目录的名称
  3. 存储在数据库表中的联系信息

我的测试网站是罗格斯大学目录(http://www.acs.rutgers.edu/directory) 我可以通过我的浏览器访问服务(帖子到http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results),但如果我尝试通过CURL发布相同的信息,则会出现404错误。

注意:如果我直接从浏览器打开该URL并且不使用它们的表单提交数据,我会得到相同的错误。

代码:
这里是他们的目录网站使用的代码:

<fieldset> 
<legend>Search For People</legend> 
<div style="width:50%;margin-left:auto;margin-right:auto;"> 
<form method="post" action="http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results" name="thisform" onkeyup="highlight()" onclick="highlight()"> 
    <p> 
     <label for="p_name_last">Last Name:&nbsp;&nbsp;[Required]</label><br> 
     <input tabindex="1" accesskey="L" class="required" type="text" id="p_name_last" name="p_name_last" size="25" maxlength="23"> 
    </p> 
    <p> 
     <label for="p_name_first">First Name:&nbsp;&nbsp;[or initial]</label><br> 
     <input tabindex="2" accesskey="f" type="text" id="p_name_first" name="p_name_first" size="25" maxlength="23"> 
    </p> 
    <input tabindex="3" type="submit" value="Search">&nbsp;&nbsp; 
</form> 
</div> 

这里是我使用卷曲的服务代码:

<?php 
$p_name_last = "doe"; 
$p_name_first = ""; 
$curlPost = 'p_name_last=' . urlencode($p_name_last) . '&p_name_first=' . urlencode($p_name_first) . '&SUBMIT=Search'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results'); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); 
if(! $result = curl_exec($ch)) 
{ 
    trigger_error(curl_error($ch)); 
} 
curl_close($ch); 
echo "<pre>"; 
print_r($result); 
?> 

任何想法或建议将不胜感激!

谢谢,
迈克

回答

3

Web服务器可以选择处理请求后发送的响应。这就是为什么你会看到404 - 这里有一个资源,但它以不直观的方式回应。

无论如何,我可以用curl贴到这个页面并得到回应;

curl http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results --data-urlencode p_name_last=test --data-urlencode p_name_first=test

如果我发布你提交= ... PARAM我得到一个404只有两个名字参数试试吧。


更新:其实,你可以作为第一个名称参数存在这么久只发送姓参数 - 第一个名称参数可以是空的。

+0

你是个天才。谢谢 – Swift 2010-08-23 20:58:21

-1

事实上http://www.acs.rutgers.edu/pls/pdb_p/Pdb_Display.search_results页面不存在。 您有重定向到新地址:http://eas.rutgers.edu,浏览器可以遵循重定向,但CURL可能有问题。

您正在尝试访问的网页已移动!

新地址:http://eas.rutgers.edu

请记得更新你的书签。

此页面将在10秒内自动重定向。 如果重定向失败,请单击http://eas.rutgers.edu继续。

您正在尝试访问的网页已移动!

新地址:http://eas.rutgers.edu

请记得更新你的书签。

此页面将在10秒内自动重定向。 如果重定向失败,请单击http://eas.rutgers.edu继续。

+0

你显然需要学习更好的阅读。 – Swift 2010-08-23 20:57:20

+0

您在PHP文件的第6行提供的URL是404,..这就是我想告诉你的。 Thx让我失望。 – Hydrino 2010-08-23 21:37:30

+0

整个问题是,它不是一个真正的404 ...阅读我的帖子10-13行。 – Swift 2010-08-24 22:02:28

1

这个问题似乎是表单的输入元件

<input tabindex="3" type="submit" value="Search"> 

不包含名称属性,因此不沿在形式SUBMIT=search POST数据发送,或者至少它不应该是,根据到http://www.w3schools.com/tags/att_input_name.asp,其中规定

“只有具有name属性的表单元素在提交表单时才会传递其值。”

我想你的尝试导致404响应,因为接受你的请求的脚本永远不会期望SUBMIT变量,这就是接受的解决方案工作的原因。

+0

恐怕你误解了。 ''是未提交值的提交按钮。它只是启动'POST'。也就是说,[W3Fools](http://w3fools.com)使用的措辞是不明确的,即“表单元素”应该被认为是来自其他输入类型的“表单值”或“表单数据”,而不是HTML元素本身。此外,[HTML Specification](https://tools.ietf.org/html/rfc1866#section-8.1.2.7)指定'name'是''的可选属性。 – verbumSapienti 2014-12-10 14:42:25