cURL获取值并传递到下一页并获得结果

时间:2015-11-30 23:17:58

标签: php post curl

我有一张表格。使用cURL我得到了这个回复:

<form id="Homepage" action="/WebApp/home.htm" method="post">
    <div id="searchElements">
        <input id="imeiNumber" type="hidden" name="imeiNumber" value=""/>
        <input type="hidden" name="tokenEncrypted" id="tokenEncrypted"  value="GQ5SA0FDIHDH6WWNR1TC3UVVUIEBFNMVYDDL7UCX9A"/>
        <input type="hidden" name="tokenTimeStamp" id="tokenTimeStamp" value="1448839083432"/>
</form>

我需要传递值imeiNumber才能获得结果。

如果使用cURL,是否需要临时保存tokenEncryptedtokenTimeStamp并将其与imeiNumber一起传递?

以下是获取上述结果的代码:

<?php
$cook = "/cookie/gsx.txt";
$site = "www.mywebsite.com";
$data = "imeiNumber=357996051632600";
$curl = curl_init('https://'.$site.'/WebApp/home.htm');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Connection: keep-alive',
    'Accept: application/json, text/javascript, */*; q=0.01',
    'Origin: https://'.$site,
    'X-Requested-With: XMLHttpRequest',
    'User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25',
    'Referer: https://'.$site.'/WebApp/resource.htm',
    )   );
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cook);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
echo $der1 = curl_exec($curl);
curl_close($curl);
?>

如何保存此值并在我的请求中传递它们?

1 个答案:

答案 0 :(得分:1)

我会像这样在返回的html上使用DOMDocument

<?php

    libxml_use_internal_errors( true );
    $dom=new DOMDocument;
    $dom->validateOnParse=false;
    $dom->recover=true;
    $dom->loadHTML( $der1 );
    libxml_clear_errors();

    $params=array(
        'imeiNumber'        =>  '357996051632600',
        'tokenEncrypted'    =>  $dom->getElementById('tokenEncrypted')->nodeValue,
        'tokenTimeStamp'    =>  $dom->getElementById('tokenTimeStamp')->nodeValue
    );
    $dom=null;

    /* params for the curl POST request */
    $params=http_build_query( $params );

    /* curl stuff */

?>