如何使用CURL构建POST

时间:2015-09-05 10:54:19

标签: php post curl

我想通过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>

这是CURL

    $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);

我哪里错了???

1 个答案:

答案 0 :(得分:0)

$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);