0

我正在设计工作应用程序。它应该将提供的数据插入到下面提供的表格中,并自动提交。在任何其他页面上都没有问题,我可以登录、保存 cookie、阅读它们、转到同一站点上的其他链接。我遇到的问题是在表单页面上发生的,具体来说,curl 执行并获取页面,但未发送发布数据。(数据作为数组发送,也尝试了 urlencoded 字符串)它认为可能有一个表单的结构方式存在问题(下面的代码)。此外,带有表单的站点不是我的,但我会提供被询问的标题和帖子变量。

所以开始吧。形式:

<form action="newFault" method="GET" id="typeForm">
<div style="margin:5px">
Tip smetnje:
<select name="type" id="type">
<option value="SVA" selected>SVA VA</option>
<option value="SNBS">SNBS - NBSA</option>
<option value="SULL">SULL - ULL</option>
</select>
</div>
</form>

<div>
<form method="POST" action="createFault" enctype="multipart/form-data">
<input type="hidden" name="type" value="SVA">
<table class="tableLight" cellpadding="0" cellspacing="1" style="margin: 5px;">
<tr>
<td colspan="2" style="text-align: right">Virtual account code:</td>
<td><input name="accountCode" size="60"></td>
<td></td>
</tr>

<tr>
<th>konos</th>
<th style="text-align: right">Kontakt osoba:</th>
<td><input name="param.konos" size="60"></td>
<td></td>
</tr>
<tr>
<th>tel</th>
<th style="text-align: right">Telefon:</th>
<td><input name="param.tel" size="60"></td>
<td></td>
</tr>
<tr>
<th>tfx</th>
<td style="text-align: right">Telefax:</th>
<td><input name="param.tfx" size="60"></td>
<td></td>
</tr>
<tr>
<th>eml</th>
<td style="text-align: right">E-mail:</td>
<td><input name="param.eml" size="60"></td>
<td></td>
</tr>
<tr>
<th>vrkv</th>
<th style="text-align: right">Vrsta:</th>
<td><input name="param.vrkv" size="60"></td>
<td></td>
</tr>
<tr>
<th>lpb</th>
<td style="text-align: right">Lokalni pozivni broj:</td>
<td><input name="param.lpb" size="60"></td>
<td></td>
</tr>
<tr>
<th>idkod</th>
<th style="text-align: right">nesto pristupa:</th>
<td><input name="param.idkod" size="60"></td>
<td></td>
</tr>
<tr>
<th>ugbrz</th>
<td style="text-align: right">brzina:</td>
<td><input name="param.ugbrz" size="60"></td>
<td></td>
</tr>
<tr>
<th>iatk</th>
<th style="text-align: right; ">Name:</th>
<td><textarea name="param.iatk" cols="40" rows="3"></textarea></td>
<td></td>
</tr>
<tr>
<th>dkk</th>
<td style="text-align: right">Datum koji odredi krajnji korisnik (ukoliko je to primjenjivo):</td>
<td><input name="param.dkk" size="60"></td>
<td></td>
</tr>
<tr>
<th>opkv</th>
<th style="text-align: right">Opis kvara:</th>
<td><textarea name="param.opkv" cols="40"></textarea></td>
<td></td>
</tr>
<tr>
<th colspan="2" style="text-align: right">Dokumentacija u TIFF formatu:</th>
<td><input type="file" name="attachment"></td>
<td></td>
</tr>
<tr>
<td colspan="2"></td>
<td colspan="2">
<input type="submit" value="Pozovi">

PHP代码:

function saljipostom()
{
$postdata = 'type=SVA&accountCode=101010&param.konos=osoba&param.tel=016000840&param.tfx=&param.eml=&param.vrkv=vrsta&param.lpb=&param.idkod=02637992641&param.ugbrz=&param.iatk=imekorisnika&param.dkk=&param.opkv=opis&attachment=&submit=Pozovi';
$fields = array(
'type'=>'SVA',
'accountCode'=>'101010',
'param.konos'=>'osoba',
'param.tel'=>'016000840',
'param.tfx'=>'',
'param.eml'=>'',
'param.vrkv'=>'vrsta',
'param.lpb'=>'',
'param.idkod'=>'02637992641',
'param.ugbrz'=>'',
'param.iatk'=>'imekorisnika',
'param.dkk'=>'',
'param.opkv'=>'opis',
'attachment'=>''
);
$polje = $fields;
foreach ( $fields as $key => $value) 
{
    $post_items[] = $key . '=' . urlencode($value);
}
$post_string = implode ('&', $post_items);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://something.something/ui/ganimed/b2b/newFault?type=SVA");
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'curl/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'curl/cookies.txt');

$result = curl_exec ($ch);
curl_close ($ch);
unset($ch);

$page = str_get_html($result);
echo $page;
}

输出回显到 .js

来自 curl 的响应标头:

HTTP/1.1 200 OK Date: Thu, 24 Jan 2013 08:05:17 GMT Content-Type: text/html;charset=UTF-8 Content-Language: en-US Vary: Accept-Encoding Transfer-Encoding: chunked

Firebug 说 post 字段是:

typeSVA
accountCode
param.konos
param.tel
param.tfx
param.eml
param.vrkv
param.lpb
param.idkod
param.ugbrz
param.iatk
param.dkk
param.opkv
attachment

萤火虫中的标题

Response Headers
Connection  Keep-Alive
Content-Language    en-US
Content-Length  0
Content-Type    text/plain
Date    Thu, 24 Jan 2013 12:52:46 GMT
Keep-Alive  timeout=15, max=100
Location    https://something.something/ui/something/b2b/faults?type=SVA&guid=40898022-1b33-42ab-a9f2-696cc5f70950

请求标头

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Cookie  JSESSIONID=DD0148C1D1701FA237704C42DE093687.node1
Host    something
Referer https://something.something/ui/something/something/newFault?type=SVA
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0

问题:我遇到的问题是在表单页面上发生的,具体来说,curl 执行并获取页面,但未发送发布数据,即。表单不接受它(数据作为数组发送,也尝试了 urlencoded 字符串)有什么想法吗?

您需要的其他任何事情都可以问。

4

2 回答 2

0
$url = 'http://foo.com/';

    $paramsarray = array(
            'name' => ' Test',
            'age' =>  24,
            'preferences' => array(1,2,3,4,5,6)
    )

$params = http_build_query($paramsarray);


$ch = curl_init();
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

    if(curl_exec($ch) === false) {
            echo 0;
    } else {
            echo 1;
    }

    curl_close ($ch);

试试这个,您可以使用该http_build_query()函数将数组转换为所需的字符串格式。然后使用 curl 发布到 url

于 2013-01-24T09:39:21.657 回答
0

创建一个 index.php 文件并在其中放置以下代码:

<?php

$url = 'http://localhost/curltest/ind.php';

$paramsarray = array(
    'name' => '',
    'age' =>  24,
);

$params = $query = http_build_query($paramsarray, '', '&');



$ch = curl_init($url);
curl_setopt_array($ch, array(
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $params,
));
$ress = curl_exec($ch);

echo $ress;

if(curl_exec($ch) === false) {
    echo 0;
} else {
    echo 1;
}
curl_close ($ch);

现在创建第二个 PHP 文件,名为ind.php. 将其放在同一个项目中。

在该文件中编写以下代码:

<?php
print_r($_POST);
?>

运行 index.php 文件,你会得到如下输出:

Array ( [name] => [age] => 24 ) 1

这表明空的 Post 字段是ind.php使用 curl 提交到页面的。

于 2013-01-24T12:31:53.943 回答