[英]Curl (php) post variables not received form submitting
我正在設計工作應用程序。 它應該將提供的數據插入下面提供的表單中,並自動提交。 在任何其他頁面上都沒有問題,我可以登錄、保存 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¶m.konos=osoba¶m.tel=016000840¶m.tfx=¶m.eml=¶m.vrkv=vrsta¶m.lpb=¶m.idkod=02637992641¶m.ugbrz=¶m.iatk=imekorisnika¶m.dkk=¶m.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 字符串)有什么想法嗎?
還有什么需要問的就行了。
$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
制作一個 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 將空的 Post 字段提交到ind.php
頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.