【问题标题】:How to build a POST With CURL如何使用 CURL 构建 POST
【发布时间】:2015-09-05 10:54:18
【问题描述】:

我想通过 Curl 发送 Post 数据来登录和解析网站页面。 我尝试通过 Curl 发送“电子邮件”和“密码”,但响应为空。我认为问题在于表单中还有其他字段,例如“记住我”,但我不知道如何在 CURL Post 中插入这些值。

这是html表单

<form action="/account/login" method="post" >
      <input type="hidden" name="targetUri" value="/a/site"/>

      <fieldset class="table">

        <table>

            <tr>
            <th>
              <label for="email">E-mail:</label>
            </th>
            <td>
              <input type="email" class="first_input value "
                id="email" name="email" size="40" value=""/>
              <div class="error">

              </div>
            </td>
          </tr>
          <tr>
            <th>
              <label for="password">Password:</label>
            </th>
            <td>
              <input type="password" class="value "
                id="password" name="password" size="40" value=""/>
              <div class="error">

              </div>
            </td>
          </tr>
            <tr>
              <th></th>
              <td>
                <div class="input">
                  <input type="hidden" name="_rememberMe" /><input type="checkbox" name="rememberMe" id="rememberMe"  />&nbsp;<span>Ricordami</span>
                </div>
              </td>
            </tr>
            <tr>
              <th></th>
              <td>
                <input class="inputsubmit input" type="submit" name="submit" value="Accedi">
              </td>
            </tr>

        </table>

      </fieldset>

这是卷曲

    $ckfile = tempnam ("/tmp", "CURLCOOKIE"); 
    $fields = array('email' => 'my@email.com', 'password' => 'mypassword',); 
    $fields_string = ''; 

    foreach($fields as $key=>$value) { 
        $fields_string .= $key . '=' . $value . '&'; 
    } 
    rtrim($fields_string, '&'); 

    $url = "https://service.threadvine.eu/account/login";
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, count($fields)); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects 
    $output = curl_exec($ch); 

    print $output;
    // Get Login page and its cookies and save cookies in the temp file 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 


    // Post login form and follow redirects 
    $url_to_grab = "https://service.threadvine.eu/a/site";
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url_to_grab); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

    $output = curl_exec($ch);

    $doc = new DOMDocument();
    $doc->loadHTML($output);

我哪里错了???

【问题讨论】:

    标签: php post curl


    【解决方案1】:
    $ckfile = tempnam ("/tmp", "CURLCOOKIE"); 
    $fields = array('email' => 'my@email.com', 'password' => 'mypassword',); 
    $fields_string = ''; 
    
    foreach($fields as $key=>$value) { 
        $fields_string .= $key . '=' . $value . '&'; 
    } 
    rtrim($fields_string, '&'); 
    
    $url = "https://service.threadvine.eu/account/login";
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, count($fields)); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Tells cURL to follow redirects 
    $output = curl_exec($ch); 
    
    print $output;
    // Get Login page and its cookies and save cookies in the temp file 
    // **$ch = curl_init(); <= Disable**
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); // Stores cookies in the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $output = curl_exec($ch); 
    
    
    // Post login form and follow redirects 
    $url_to_grab = "https://service.threadvine.eu/a/site";
    // **$ch = curl_init(); <= Disable**
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Accepts all CAs 
    curl_setopt($ch, CURLOPT_URL, $url_to_grab); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); //Uses cookies from the temp file 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    
    $output = curl_exec($ch);
    
    $doc = new DOMDocument();
    $doc->loadHTML($output);
    

    【讨论】:

    • 你能解释一下你的答案吗?撇开您没有真正回答“我错在哪里???”问题不谈,只有代码的答案会使您的答案很容易被删除,因为它无助于其他人与相应代码的解释清楚的答案一样多。 “教人钓鱼”等等。
    • 谢谢,但还没有解决。只是结果保持为空。如果它对我有用,我正在运行 PHP 5.6.10,我也尝试过 5.4.42。
    • 好的,我刚刚发现问题可能不在 CURL 代码中,但我通过 chrome 控制台看到,只有在会话已经启动时,表单才允许登录,所以我必须启动一个我认为 CURL 之前的会话。我会更新!
    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 2020-10-18
    相关资源
    最近更新 更多