curl无法正确发送标头

时间:2014-10-05 20:27:06

标签: php curl

我的初始项目是检查某个Apple ID是否存在,我已经在php中通过导航到apppleid.apple.com/account/并假装注册一个除了帐户之外的所有字段都空白的帐户字段,如果我收到错误,则表示该帐户已存在,否则如果我收到其他错误但不存在“帐户存在”错误,则返回false。但是我在途中遇到了一些问题。第一个是你需要保留所有标题/ cookie(我做过)但它仍然不起作用,显然总是以“1”回答。代码可以在这里找到:PASTEBIN。请按照链接尝试解决这个问题,我真的需要这样做。非常感谢,无论谁有时间阅读这篇文章。

修改 代码:

<?php
require("simplehtmldom_1_5/simple_html_dom.php");

$input = get_data('https://appleid.apple.com/account');
$html = new simple_html_dom();
$html->load($input);

//echo $input;
$table = array();
foreach($html->find('input') as $inn)
{
        $val = "";
        try
        {
                $val = $inn->getAttribute('value');    
        }
        catch (Exception $e)
        {
                $val = "";
        }
        //echo $inn->getAttribute('name') . $val . "\n";
        if($inn->getAttribute('name') != "" && $inn->getAttribute('name') != "account.name")
        {
                $table[$inn->getAttribute("name")] = $val;
        }
        if($inn->getAttribute('name') == "account.name")
        {
                $table[$inn->getAttribute("name")] = "naclo3samuel@gmail.com";
        }
}
$NIX = http_build_query($table);
//set the url, number of POST vars, POST data
$ch = curl_init();
$hs = get_headers("https://appleid.apple.com/account", 0);
$headers = $hs;
curl_setopt($ch,CURLOPT_URL, "https://appleid.apple.com/account/");
curl_setopt($ch,CURLOPT_POST, count($NIX));
curl_setopt($ch,CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch,CURLOPT_POSTFIELDS, $NIX);

//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);



/* gets the data from a URL */
function get_data($url) {
        $ch = curl_init();
        $timeout = 5000;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
}
?>

1 个答案:

答案 0 :(得分:-1)

其中一个问题就在这里:get_headers("https://appleid.apple.com/account", 0);

这将返回如下内容:

[0] => HTTP/1.1 200 OK
[1] => Date: Sat, 29 May 2004 12:28:13 GMT
[2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
[3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => ETag: "3f80f-1b6-3e1cb03b"
[5] => Accept-Ranges: bytes
[6] => Content-Length: 438
[7] => Connection: close
[8] => Content-Type: text/html

cURL应该怎么做?这不是CURLOPT_HTTPHEADER可接受的格式,也不是服务器对客户端请求所期望的标头。

我想你正试图建立Cookie会话。我建议您在不使用get_headers()的情况下完成所有操作,或者将手指放在标题中。

通过设置CURLOPT_COOKIEJARCURLOPT_COOKIEFILE选项启用cURL的Cookie支持,拨打https://appleid.apple.com/account来初始化Cookie,然后完成其余操作。

示例:

$cookies = tmpfile();

$curl = curl_init();
curl_setopt_array($curl, [
    CURLOPT_URL => "https://appleid.apple.com/account/",
    CURLOPT_COOKIEJAR => $cookies,
    CURLOPT_COOKIEFILE => $cookies,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true
]);
curl_exec();

curl_setopt_array($curl, [
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $NIX
]);
$result = curl_exec($ch);

$hsize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = explode("\r\n", substr($result, 0, $hsize));
$result = substr($result, $hsize);