PHP cURL拒绝POST到网站

时间:2015-04-20 17:21:19

标签: php post curl

我想请求一些帮助。我已经尝试了几个小时阅读不同的文章和Q&至于为什么PHP cURL拒绝POST的可能的治愈方法。但我尝试的方法都没有给出任何结果。

我试图发布的网站是“pixiv.net”。这是我目前的PHP代码:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/pixiv-cookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/pixiv-cookie.txt');

    curl_setopt($ch, CURLOPT_URL, 'http://www.pixiv.net/');

    curl_setopt($ch, CURLOPT_HEADER, 1);

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36');
    curl_setopt($ch, CURLOPT_REFERER, 'http://www.pixiv.net');

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    echo curl_exec($ch);

    if (curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
    } else {
        echo 'Operation completed without any errors<br>'; }

    //--------------------------------------------

    curl_setopt($ch, CURLOPT_URL, 'https://www.secure.pixiv.net/login.php/');

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $httpHeader = array(
        'Host: www.pixiv.net',
        'Connection: keep-alive',
        'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
        'DNT: 1',
        'Referer: http://www.pixiv.net',
        'Accept-Encoding: gzip, deflate, sdch',
        'Accept-Language: en-US,en;q=0.8,ja;q=0.6,zh-CN;q=0.4,zh;q=0.2,fr-FR;q=0.2,fr;q=0.2'
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);

    curl_setopt($ch, CURLOPT_POST, 1);
    $postData='
        mode=login
        &return_to=%2F
        &pixiv_id=#####
        &pass=#####
        &skip=1
    ';
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

    echo curl_exec($ch);

    curl_close($ch);
    unset($ch);
?>

在网站上,登录区域的HTML部分如下:

<form action="https://www.secure.pixiv.net/login.php" method="post" data-time="1429509525" data-text-confirm="...">
<input type="hidden" name="mode" value="login">
<input type="hidden" name="return_to" value="/">
<div class="id"><input type="text" class="js-placeholder" id="login_pixiv_id" name="pixiv_id" value="" maxlength="255" placeholder="pixiv ID or Email"></div>
<div class="pass-signin"><input type="password" class="js-placeholder" id="login_password" name="pass" value="" maxlength="32" placeholder="Password"><input type="submit" id="login_submit" value="Login" class="ui-button"></div>
<div class="save-forgotpass"><label><input name="skip" type="checkbox" checked="" value="1">Remember me</label>
<div><a href="https://www.secure.pixiv.net/reminder.php" class="reminder">Forgot ID or password?</a>
<a href="https://www.secure.pixiv.net/login.php?return_to=%2F" class="ssl-login">Secure sign-in through SSL (https)</a></div>
</div></form>

到目前为止,GET可以很好地保存网站上的cookie。我将HTTP标头设置为与我当前的浏览器完全相同,以及其他一些有助于欺骗连接的东西。我在这里删除了我的登录详细信息,但我保证他们是正确的。

网站返回的标题是:

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 20 Apr 2015 18:59:09 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Host-Time: 113
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-Frame-Options: SAMEORIGIN

HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 20 Apr 2015 19:03:25 GMT
Content-Type: text/html
Content-Length: 564
Connection: keep-alive

有没有人知道我可能做错了什么?如果有帮助,网站会加载(GET)他们自己的一些图像,这些图像在浏览器的控制台中显示为403(可能是因为网站检测到它不是同一个域请求)。这个PHP页面在XAMPP上本地运行,我没有从cURL得到任何错误。

3 个答案:

答案 0 :(得分:2)

更新2

第一次请求

请求标题http://www.pixiv.net/

GET / HTTP/1.1
Accept-Encoding: deflate, gzip
Host: www.pixiv.net
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache

回复标题http://www.pixiv.net/

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 21 Apr 2015 01:12:07 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Host-Time: 113
Set-Cookie: PHPSESSID=c40dbb30394766ec885c61f7e08e00fb; expires=Tue, 21-Apr-2015 02:12:07 GMT; Max-Age=3600; path=/; domain=.pixiv.net
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: p_ab_id=2; expires=Tue, 21-Apr-2020 01:12:07 GMT; Max-Age=157852800; path=/; domain=.pixiv.net
Set-Cookie: p_ab_id=2; expires=Tue, 21-Apr-2020 01:12:07 GMT; Max-Age=157852800; path=/; domain=.pixiv.net
X-Frame-Options: SAMEORIGIN
Content-Encoding: gzip

第二次请求

请求标题https://www.secure.pixiv.net/login.php

这有来自之前回复的cookie。

POST /login.php HTTP/1.1
Accept-Encoding: deflate, gzip
Host: www.secure.pixiv.net
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Cookie: PHPSESSID=c40dbb30394766ec885c61f7e08e00fb; p_ab_id=2
Content-Length: 56
Content-Type: application/x-www-form-urlencoded

回复标题https://www.secure.pixiv.net/login.php

重定向到http://www.pixiv.net/(请参阅标题中的Location), 然后http://www.pixiv.net/重定向到http://www.pixiv.net/mypage.php

它没有改变cookie,所以我跳过http://www.pixiv.net/和 何时到http://www.pixiv.net/mypage.php

HTTP/1.1 302 Found
Server: nginx
Date: Tue, 21 Apr 2015 01:12:08 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 0
Connection: keep-alive
X-Host-Time: 62
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
P3P: CP="THIS IS NOT P3P"
Set-Cookie: PHPSESSID=509719_83607aedd2945280c7879fab036c5fb0; expires=Tue, 21-Apr-2015 02:12:08 GMT; Max-Age=3600; path=/; domain=.pixiv.net
Set-Cookie: device_token=d14a832ce615d42daae571775215d712; expires=Thu, 21-May-2015 01:12:08 GMT; Max-Age=2592000; path=/; domain=.pixiv.net
Location: http://www.pixiv.net/

注意:响应标题的最后一行Location: http://www.pixiv.net/ 这是浏览器重定向到的URL。

但是我跳过了Location: http://www.pixiv.net/,因为它没有设置新的Cookie。

http://www.pixiv.net/刚刚创建了另一个302重定向到http://www.pixiv.net/mypage.php

第三次请求

请求标题http://www.pixiv.net/mypage.php

GET /mypage.php HTTP/1.1
Accept-Encoding: deflate, gzip
Host: www.pixiv.net
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Cache-Control: no-cache
Pragma: no-cache
Cookie: PHPSESSID=509719_83607aedd2945280c7879fab036c5fb0; p_ab_id=2; device_token=d14a832ce615d42daae571775215d712

回复标题http://www.pixiv.net/mypage.php

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 21 Apr 2015 01:12:08 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
X-Host-Time: 62
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: module_orders_mypage=%5B%7B%22name%22%3A%22everyone_new_illusts%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22spotlight%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22featured_tags%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22contests%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22following_new_illusts%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22mypixiv_new_illusts%22%2C%22visible%22%3Atrue%7D%2C%7B%22name%22%3A%22booth_follow_items%22%2C%22visible%22%3Atrue%7D%5D; expires=Wed, 20-Apr-2016 01:12:08 GMT; Max-Age=31536000; path=/; domain=.pixiv.net
X-Frame-Options: SAMEORIGIN
Content-Encoding: gzip

最终PHP代码

从302重定向中获取新Cookie。使用302网址发出第三个请求。

$request = array();
$request[] = "Host: www.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$url = 'http://www.pixiv.net/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
$info = var_export(curl_getinfo($ch),true);
$fp = fopen('pixiv.html','w');
fwrite($fp,"$data\n\n$info");
fclose($fp);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $e = 0;
  while(true){
    $s = strpos($head,'Set-Cookie: ',$e);
    if (!$s){break;}
    $s += 12;
    $e = strpos($head,';',$s);
    $cookie = substr($head,$s,$e-$s) ;
    $s = strpos($cookie,'=');
    $key = substr($cookie,0,$s);
    $value = substr($cookie,$s);
    $cookies[$key] = $value;

  }
 $cookie = '';
 $delim = '';
 foreach ($cookies as $k => $v){
   $cookie .= "$delim$k$v";
   $delim = '; ';
 }
}

$request = array();
$request[] = "Host: www.secure.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$request[] = "Cookie: $cookie";
$postData = 'mode=login&return_to=%2F&pixiv_id=username&pass=password';
$url = 'https://www.secure.pixiv.net/login.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $info = var_export(curl_getinfo($ch),true);
  $fp = fopen('pixivLogin.html','w');
  fwrite($fp, "\n------------------$data\n\n$info");
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $e = 0;
  while(true){
    $s = strpos($head,'Set-Cookie: ',$e);
    if (!$s){break;}
    $s += 12;
    $e = strpos($head,';',$s);
    $cookie = substr($head,$s,$e-$s) ;
    $s = strpos($cookie,'=');
    $key = substr($cookie,0,$s);
    $value = substr($cookie,$s);
    $cookies[$key] = $value;

  }
 $cookie = '';
 $delim = '';
 foreach ($cookies as $k => $v){
   $cookie .= "$delim$k$v";
   $delim = '; ';
 }
}


$request = array();
$request[] = "Host: www.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$request[] = "Cookie: $cookie";
$url = 'http://www.pixiv.net/mypage.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
$info = var_export(curl_getinfo($ch),true);
$fp = fopen('pixiv2.html','w');
fwrite($fp,"$data\n\n$info");
fclose($fp);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);
  $data = substr($data,$skip);
  echo $data;

更新结束2


更新

每次登录时都会通过获取一个用于登录的新cookie来开始新的登录。

这将转到http://www.pixiv.net/并获取登录的cookie。
为我工作得很好。

$request = array();
$request[] = "Host: www.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$url = 'http://www.pixiv.net/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {

获取响应头:

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $head = substr($data,0,$skip);

查找所有Set-Cookie和所有Cookie:

    $e = 0;
    while(true){
      $s = stripos($head,'Set-Cookie: ',$e);
      if (!$s){break;}
      $s += 12;
      $e = strpos($head,';',$s);
      $cookie = substr($head,$s,$e-$s) ;
      $s = strpos($cookie,'=');
      $key = substr($cookie,0,$s);
      $value = substr($cookie,$s);
      $cookies[$key] = $value;
    }

然后将$ cookies数组转换回字符串:

    $cookie = '';
    $delim = '';
    foreach ($cookies as $k => $v){
      $cookie .= "$delim$k$v";
      $delim = '; ';
    }
  }

登录:

$request = array();
$request[] = "Host: www.secure.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";

使用第一次请求的Cookie:

$request[] = "Cookie: $cookie";
$postData = 'mode=login&return_to=%2F&pixiv_id=username&pass=password';
$url = 'https://www.secure.pixiv.net/login.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
$head = substr($data,0,$skip);
$data = substr($data,$skip);
echo $data;

更新结束


我直接去了:

https://www.secure.pixiv.net/login.php

使用http://www.pixiv.net/

中的Cookie

肯定postData错了:

   $postData='
        mode=login
        &return_to=%2F
        &pixiv_id=#####
        &pass=#####
        &skip=1
    ';

添加所有空格和换行符:

        mode=login
        &return_to=%2F
        &pixiv_id=#####
        &pass=#####

这样做:

$postData = 'mode=login&return_to=%2F&pixiv_id=username&pass=password';

我没有使用这些:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.pixiv.net');

我将cookie添加到HTTP标头:

$request[] = "Cookie:PHPSESSID=b2e3185bd045475a2174fb28fb642569; 

这些我用以防万一有麻烦而且不需要。

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);

此工作

$request = array();
$request[] = "Host: www.secure.pixiv.net";
$request[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
$request[] = "Cookie:PHPSESSID=b2e3185bd045475a2174fb28fb642569; p_ab_id=3";
$post = 'mode=login&return_to=%2F&pixiv_id=username&pass=password';
$url = 'https://www.secure.pixiv.net/login.php';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_ENCODING,"");

curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);


$data = curl_exec($ch);
if (curl_errno($ch)){
    $data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
  $info = rawurldecode(var_export(curl_getinfo($ch),true));
 // Get the cookies:

  $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
  $requestHeader= substr($data,0,$skip);
  $data = substr($data,$skip);
  echo $data;

}

答案 1 :(得分:1)

发布以确保您有正确的标头集。您的变量$ httpHeader定义为具有相同键'0'的所有属性,因此会相互覆盖。

答案 2 :(得分:1)

您发送的自定义主机:标头与您正在使用的网址的主机名不匹配,这是可疑的并且很可能是错误的。

我建议你不要设置自定义主机:标题,但让curl为此做出自己的魔力,这可能是你想要的。