卷曲(php)发布变量未收到表单提交

时间:2013-01-24 09:31:36

标签: php post curl

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

开始吧。 形式:

<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说帖子字段是:

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字符串)任何想法?

你需要的任何其他东西。

2 个答案:

答案 0 :(得分: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

发布到网址

答案 1 :(得分:0)

将index.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);
?>

现在将名为ind.php的secod php文件放在同一个项目中

在该文件中写下以下代码。

<?php
 print_r($_POST);
?>

运行index.php文件,输出

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

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