php curl post data to get value form refer url

时间:2015-04-26 09:24:08

标签: php session curl cookies libcurl

我想将帖子数据发送到页面以获取价值。问题是我想从abc.com获取数据。只有当用户从abc.com abc.com来到abc.com/previous url时才会打开url has be moved

也许正在做一些会话或cookie来进行身份验证。我不知道。

我想要做的是从abc.com获取数据。有没有办法通过卷曲来做到这一点?

我的PHP代码:

当您运行代码$SearchBy = "2"; $AttorneySearchMode="Name"; $LastName= "Smith"; $FirstName= "William"; $CaseStatusType= "0"; $SortBy= "fileddate"; echo $url = 'https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600%2c601%2c602%2c603%2c604%2c605%2c606%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c616%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704%2c705%2c706%2c707%2c708%2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729%2c730%2c731%2c797%2c798&NodeDesc=All+Courts'; //set POST variables $fields = array( 'SearchBy' => urlencode($SearchBy), 'AttorneySearchMode' => urlencode($AttorneySearchMode), 'LastName' => urlencode($LastName), 'FirstName' => urlencode($FirstName), 'CaseStatusType' => urlencode($CaseStatusType), 'SortBy' => urlencode($SortBy), ); $fields_string = ""; foreach ($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } rtrim($fields_string, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, true); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); $result = curl_exec($ch); curl_close($ch); print_r($result); 时,对于此网址,您需要来自旧的引用网址,然后才能在浏览器中使用

<div>
<table>
    <thead>
        <!-- Example with only 2 columns -->
        <tr>
            <th>Col1</th>
            <th>Col2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>foo</td>
            <td>this</td>
        </tr>
        <tr>
            <td>bar</td>
            <td>that</td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

这是一个有一些小修改的起点。

我发现3个问题阻止我在他们的服务器上请求页面:

  1. 需要在cURL中启用Cookie并在发布
  2. 之前获取一页
  3. 需要禁用SSL检查(CURLOPT_SSL_VERIFYPEERCURLOPT_SSL_VERIFYHOST
  4. 请求中缺少一些必要的表单字段
  5. 在下面的示例中,我首先获取主搜索页面,然后重新使用相同的cURL句柄发布表单。请注意,这是不完整的,您需要编写代码来填充剩余的表单字段。

    // first fetch search page to set cookies
    $url = 'https://www.clarkcountycourts.us/Anonymous/Search.aspx?ID=400&NodeID=101%2c103%2c104%2c105%2c500%2c600%2c601%2c602%2c603%2c604%2c605%2c606%2c607%2c608%2c609%2c610%2c611%2c612%2c613%2c614%2c615%2c616%2c617%2c618%2c619%2c699%2c700%2c701%2c702%2c703%2c704%2c705%2c706%2c707%2c708%2c709%2c710%2c711%2c712%2c713%2c714%2c715%2c716%2c717%2c718%2c719%2c720%2c721%2c722%2c723%2c724%2c725%2c726%2c727%2c728%2c729%2c730%2c731%2c797%2c798&NodeDesc=All+Courts';
    
    $ch = curl_init();
    
    // needed to disable SSL checks for this site
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
    
    // enable cookie handling (they aren't saved after cURL is closed)
    curl_setopt($ch,CURLOPT_COOKIEFILE, '');
    
    // debugging
    curl_setopt($ch,CURLOPT_VERBOSE, 1);
    curl_setopt($ch,CURLOPT_STDERR, fopen('php://output', 'w'));
    
    // helpful options
    curl_setopt($ch,CURLOPT_AUTOREFERER, true);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    
    // set first URL
    curl_setopt($ch,CURLOPT_URL, $url);
    
    // execute first request to establish cookies & referer    
    $data = curl_exec($ch);
    
    // TODO: extract hidden form fields/values from form 
    $fields_string = '...';
    
    // now make second request
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    
    $result = curl_exec($ch);
    curl_close($ch);
    var_dump($result);
    

    查看我的其他一些答案linked here,其中显示了如何提取其他表单字段并执行多个请求。另请参阅(cURL -- cookies and sessions)和(PHP Curl - Cookies problem

    希望有所帮助,回答问题/问题。