PHP-如何在另一个网站上自动发布表单并解析结果

PHP-如何在另一个网站上自动发布表单并解析结果,php,curl,Php,Curl,我不熟悉屏幕上的围巾和卷发。我计划创建一个网站,就像现在正在做的那样,允许用户从网站中提取出发地、目的地和日期。然后网站将航班时刻表和机票价格返回给用户。以下是我目前的代码: 代码: 以上内容未返回亚洲航空公司的任何结果。所以我需要一些指导来继续我的任务。谢谢您的查询字符串$post\u String是正确的,但是您在发送curl之前没有在它前面加上?。请尝试以下操作: curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "?".$post_s

我不熟悉屏幕上的围巾和卷发。我计划创建一个网站,就像现在正在做的那样,允许用户从网站中提取出发地、目的地和日期。然后网站将航班时刻表和机票价格返回给用户。以下是我目前的代码:

代码:



以上内容未返回亚洲航空公司的任何结果。所以我需要一些指导来继续我的任务。谢谢

您的查询字符串
$post\u String
是正确的,但是您在发送curl之前没有在它前面加上
。请尝试以下操作:

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, "?".$post_string);
更新 这起作用了:

$request = array();
$request[] = "Host: mobile.airasia.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

$post = 'hash=61582ddd1b6ab8782ad63f1a6c6c1e46&trip-type=round-trip&origin=PEK&destination=SGN&date-depart-d=25&date-depart-my=2015-04&date-return-d=30&date-return-my=2015-04&passenger-count=1&child-count=0&infant-count=0&currency=MYR&depart-sellkey=&return-sellkey=&depart-details-index=&return-details-index=&depart-faretype=&return-faretype=&action=search&btnSearch=Search';
$url = 'https://mobile.airasia.com/en/search';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);


$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $info = rawurldecode(var_export(curl_getinfo($ch),true));
 // Get the cookies:

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $requestHeader= substr($data,0,$skip);
  $data = substr($data,$skip);
  echo $data
$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
请求头

POST /en/search HTTP/1.1
Accept-Encoding: deflate, gzip
Host: mobile.airasia.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 366
Content-Type: application/x-www-form-urlencoded
响应头:

HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Cache-control: no-cache="set-cookie"
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Date: Mon, 20 Apr 2015 04:02:14 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Server: redishot
Set-Cookie: locale=en; expires=Mon, 20-Apr-2015 05:02:11 GMT; path=/; secure
Set-Cookie: currency=MYR; expires=Mon, 20-Apr-2015 05:02:11 GMT; path=/; secure
Set-Cookie: PHPSESSID=p8mjtiiga4615pnhuu6vl1htiqkqsn7v; path=/; HttpOnly
Set-Cookie: AWSELB=CDFDE3A70C862943856FF6079178A94249700C674BDFF1E117C02BF52443FE13448AB71BEA2EA3F41C01293A39C3579A0A03905034DA565F71B4820BD1807C5558B22ED5E0;PATH=/;MAX-AGE=1800
Vary: Accept-Encoding
transfer-encoding: chunked
Connection: keep-alive
这是您从返回的HTML中需要的数据

  • 航班号
  • 飞行时间
  • 成本


不能卷曲
http://booking.airasia.com/search.aspx
因为它需要javaScript。

你必须使用移动网站。使用浏览器查看HTTP请求和响应头时,请在浏览器上禁用JavaScript。

用法:

https://mobile.airasia.com/en/search
问题是移动站点现在运行不正常,并表示稍后再试。所以我不能再往前走了。

关于这个职位

这是搜索发布的内容:

Content-Type: application/x-www-form-urlencoded
Content-Length: 366
hash=26edce4024c5611451a2a95a74e2bf01
&trip-type=round-trip
&origin=KUL
&destination=OOL&date-depart-d=20
&date-depart-my=2015-04&date-return-d=25
&date-return-my=2015-04
&passenger-count=1
&child-count=0&infant-count=0
&currency=MYR
&depart-sellkey=
&return-sellkey=
&depart-details-index=
&return-details-index=
&depart-faretype=
&return-faretype=
&action=search
&btnSearch=Search
因为他们的表单是
application/x-www-form-urlencoded
,所以您的$post\u字符串几乎是正确的。您可以对post数据使用数组,但如果值是数组,则内容类型标题将设置为
多部分/表单数据
,这应该是可以的。

因为它是
application/x-www-form-urlencoded
,所以必须使用urlencode
$post\u string

$post_string` = urlencode(implode ('&', $post_items));
要获取cookies,您不需要,也可能永远不会需要:

curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, False);
将其移除。

您将获得重定向,可能需要cookie jar:

curl_setopt($ch,CURLOPT_COOKIEFILE, "/tmp/cookie.txt") 

您可能需要设置请求标头以匹配浏览器请求:

创建一个数组以放置请求头键值
在请求数组中准确填写上传请求头中的内容。

示例:

$request = array();
$request[] = "Host: mobile.airasia.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

$post = 'hash=61582ddd1b6ab8782ad63f1a6c6c1e46&trip-type=round-trip&origin=PEK&destination=SGN&date-depart-d=25&date-depart-my=2015-04&date-return-d=30&date-return-my=2015-04&passenger-count=1&child-count=0&infant-count=0&currency=MYR&depart-sellkey=&return-sellkey=&depart-details-index=&return-details-index=&depart-faretype=&return-faretype=&action=search&btnSearch=Search';
$url = 'https://mobile.airasia.com/en/search';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);


$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $info = rawurldecode(var_export(curl_getinfo($ch),true));
 // Get the cookies:

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $requestHeader= substr($data,0,$skip);
  $data = substr($data,$skip);
  echo $data
$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
添加到卷曲:

curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

检查浏览器中的标题

然后我使用FireFox Inspector或Chrome开发工具

我转到“网络”选项卡

在FireFox中,我进入设置并打开“启用持久日志”
在Chrome中,我单击网络选项卡上的“保留日志”

然后我使用浏览器去我想去的任何地方

现在我可以看到每个请求和响应,包括重定向,并将它们与保存头进行比较。

检查:逐步检查

  • 在“选择检查图元”上单击鼠标右键
  • 选择网络选项卡
  • 刷新页面
  • 选择文档(chrome)或HTML(firefox)
  • 清除列表
  • 发布你的上传
  • 在请求列表中选择上载请求

我使用FireFox和用户代理切换器,使用旧的Motorola用户代理检索标题和HTML。然后我在curl的
HTTPHEADER

request[] = 'User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0

当我尝试回显$result时,上述情况可能(不太可能)导致错误

;它会打印整个亚洲航空的页面,而不是返回我的航班信息。这是航班信息的另一种方式吗?
上显示的内容https://booking.airasia.com/search.aspx“
将在
$result
中恢复,即卷曲本身的行为。您可以通过在
search.aspx
编辑和打印所需的结果,或者创建一个新页面来提供所需的结果,从而获得所需的结果。如果不清楚,请再次注释。您可以使用html_dom_解析器类从html中查找所有值。这是一个好类:“”不包含任何航班信息。它只包含搜索表单。@mex cheng,您正在尝试的是一个IE,而不是API端点。这不是我看不到airasia.com API可用的方式。CURL不是那样工作的……请搜索CURL文档。您的请求应该是这样的:您认为堆栈溢出是由一群傻瓜使用的吗。它不是一个板形式继承人为您的任务,他会给你的工作代码。你也可以试试谷歌,为什么不呢?你得到了什么样的回应?您是否将您的请求和响应头与我的帖子中解释的浏览器中的请求和响应头进行了比较?帖子数据没有填写在表单中并提交。您了解HTTP请求和响应头吗?在尝试执行此操作时,了解HTTP头是必要的。我已经给你看了一张我要工作的照片,只是为了确认这是可能的。让我们来看看。
request[] = 'User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0