Curl(php)帖子变量未通过提交提交

问题描述 投票:0回答:2

我正在设计工作应用程序。它应该将提供的数据插入下面提供的表格中,然后自动提交。在其他页面上没有任何问题,我可以登录,保存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说发布字段是:

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字符串)是什么想法?

您需要问的其他事项。

php post curl
2个回答
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);

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

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

<?php
print_r($_POST);
?>

运行index.php文件,您将获得如下输出:

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

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


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将其发布到网址

© www.soinside.com 2019 - 2024. All rights reserved.