Hello fellow programmers.
I am beginner programmer and I have failed attempt into creating a script for uploading image on one specific site.
My boss needs to upload images to specific site. To speed things up I need to create a script for that.
I usually use multi-part form data with cURL, but this site it is different.
Here are the headers
POST /upload-new.php?banner_url=http%3A%2F%2Ftest.com&ad_type=1&banner_size= HTTP/1.1
Host: admin.domain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: lv,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/octet-stream
X-File-Name: indonesia.gif
X-File-Size: 15450
X-File-Type: image/gif
X-File-Date: Fri, 29 May 2015 09:48:22 GMT
X-Requested-With: FileDrop-XHR-FileAPI
Referer: https://admin.domain.com/campaigns-edit.php
Content-Length: 15450
Cookie: goals=
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
GIF89a,รบ
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 Jun 2015 16:22:08 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
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
Content-Encoding: gzip
----------------------------------------------------------
https://admin.domain.com/data/tmp-uploads/159310_20150613122208_indonesia.gif
GET /data/tmp-uploads/159310_20150613122208_indonesia.gif HTTP/1.1
Host: admin.domain.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
Accept: image/png,image/*;q=0.8,*/*;q=0.5
Accept-Language: lv,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Referer: https://admin.domain.com/campaigns-edit.php
Cookie: goals=
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 13 Jun 2015 16:22:08 GMT
Content-Type: image/gif
Content-Length: 15450
Last-Modified: Sat, 13 Jun 2015 16:22:08 GMT
Connection: keep-alive
Etag: "557c58b0-3c5a"
Accept-Ranges: bytes
This is what I tried
$file = realpath($file_name);
$finfo = new finfo(FILEINFO_MIME);
$mimetype = $finfo->file($file);
$cfile = curl_file_create($file, $mimetype);
$PostData = array( '' => $cfile );
//$headers
$headers = array();
$headers[] = 'X-File-Name: indonesia.gif';
$headers[] = 'X-File-Size: 15450';
$headers[] = 'X-File-Type: image/gif';
$headers[] = 'X-File-Date: Fri, 29 May 2015 09:48:22 GMT';
$headers[] = 'X-Requested-With: FileDrop-XHR-FileAPI';
$URL = "https://admin.domain.com/upload-new.php?banner_url=http%3A%2F%2Ftest.com&ad_type=1&banner_size="
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$PostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$size = filesize($lfile);
$file = fopen($lfile, 'r');
curl_setopt($ch, CURLOPT_POSTFIELDS, "#" . $lfile);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
If (StrLen ($Proxy) > 0)
{
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_PROXY,$Proxy);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
curl_close($ch);
Website answers with link to image which gives me 404. So seems like I am failing to send over the file.
Can anybody please give me some guidance?
I would like to ask for some kind assistance. I've been trying for hours reading different articles and Q&As for a possible cure as to why PHP cURL refuses to POST. But none of the methods I try are giving any results.
The website I am attempting to POST to is "pixiv.net". Here is my current PHP code:
<?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);
?>
On the website, the HTML portion of the login area is as follows:
<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>Forgot ID or password?
Secure sign-in through SSL (https)</div>
</div></form>
So far, the GET works fine in saving the cookie from the website. I set the HTTP Headers to be the exact same as my current browser's, as well as some other things to help spoof the connection. I blanked out my login details here, but I assure you that they're correct.
The header returned by the website is:
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
Does anybody have an idea as to what I could be doing wrong? If it helps, the website does load (GET) some of their own images, which show as 403's in my browser's console (probably because the website detected that it's not a same-domain request). This PHP page is being run locally on XAMPP, and I'm not getting any errors from cURL.
Update 2
First request
Request header (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
Response header (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
Second request
Request header (https://www.secure.pixiv.net/login.php)
This has a cookie from previous response.
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
Response header (https://www.secure.pixiv.net/login.php)
This redirects to http://www.pixiv.net/ (See Location in headers),
then http://www.pixiv.net/ redirects to http://www.pixiv.net/mypage.php.
It did not alter the cookies so I skipped http://www.pixiv.net/ and
when right to 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/
NOTICE: Last line of Response Header Location: http://www.pixiv.net/
This is the URL the Browser is being redirected to.
but I skipped Location: http://www.pixiv.net/ because it did not set new cookies.
http://www.pixiv.net/ just created another 302 redirect to http://www.pixiv.net/mypage.php
Third request
Request header (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
Response header (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
FINAL PHP CODE
Get new cookies from a 302 redirect. Makes third request with the 302 URL.
$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;
end of update2
UPDATE
This starts fresh every login by getting a new cookie to use for login each time it is executed.
This goes to http://www.pixiv.net/ and gets the cookie for the login.
Worked well for me.
$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 {
Gets the response header:
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
Finds all Set-Cookie and all the cookies:
$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;
}
Then convert $cookies array back to a string:
$cookie = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
}
Do Login:
$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";
With cookie from first request:
$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;
end of update
I went directly to:
https://www.secure.pixiv.net/login.php
Using the cookie from http://www.pixiv.net/
For sure postData was wrong:
$postData='
mode=login
&return_to=%2F
&pixiv_id=#####
&pass=#####
&skip=1
';
It add all the spaces and newline characters:
mode=login
&return_to=%2F
&pixiv_id=#####
&pass=#####
do it like this:
$postData = 'mode=login&return_to=%2F&pixiv_id=username&pass=password';
I did not use these:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.pixiv.net');
I added the cookie to the HTTP Header:
$request[] = "Cookie:PHPSESSID=b2e3185bd045475a2174fb28fb642569;
These I use just in case there is trouble and are not needed.
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
This Worked
$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;
}
To post make sure you have a proper header set. your variable $httpHeader is defined with all the attributes having the same key '0' and therefore are overwritten each other.
You're sending in a custom Host: header which doesn't match the host name of the URL you're using, which is suspicious and most likely wrong.
I'd advice you to not set a custom Host: header but let curl do its own magic for that, which probably is what you want.
I am getting some info from a https web server using PHP plus cURL. All info got as HTTP GET is ok but when I need to do some HTTP POST I get a no sense output. Web server is ok as If i get the info from a web browser all works ok.
I am using following code:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
if($method == "POST"){
print_r($post_fields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch,CURLOPT_HTTPHEADER, array (
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"
));
}
if ($usecookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}
if ($refer != "") {
curl_setopt($ch, CURLOPT_REFERER, $refer );
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
Answer header is:
HTTP/1.1 200 OK
Date: Sun, 20 Nov 2011 11:04:39 GMT
Server: Apache
Cache-Control: must-revalidate
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
max-age: Thu, 01 Jan 1970 00:00:00 GMT
X-Powered-By: Servlet/2.4 JSP/2.0
idWl: PRO-LOW16_6604
Vary: Accept-Encoding
Content-Encoding: gzip
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
Any idea about where the problem could be?
It clearly shows its gziped...
Content-Encoding: gzip
Transfer-Encoding: chunked
Passing the returned data through the below function will inflate it back to readable content.
function gzdecoder($d){
$f=ord(substr($d,3,1));
$h=10;$e=0;
if($f&4){
$e=unpack('v',substr($d,10,2));
$e=$e[1];$h+=2+$e;
}
if($f&8){
$h=strpos($d,chr(0),$h)+1;
}
if($f&16){
$h=strpos($d,chr(0),$h)+1;
}
if($f&2){
$h+=2;
}
$u = gzinflate(substr($d,$h));
if($u===FALSE){
$u=$d;
}
return $u;
}