PHP cURL refuses to POST to website - php
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.
Related
Soap Curl request error - 400 Bad Request
I am making soap curl request and here is the code: $headers = array( "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"http://tempuri.org/Save_ExMsgInfo2\"", "Content-length: ".strlen($xml_post_string), ); $ch= curl_init(); curl_setopt($ch, CURLOPT_URL, "https://xxx.yyy.com:517/zzz.asmx" ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLINFO_HEADER_OUT, true); if(curl_exec($ch) === false) { $err = 'Curl error: ' . curl_error($ch); curl_close($soap_do); return $err; } else { curl_close($ch); return 'Operation completed without any errors'; } But getting below error HTTP/1.1 400 Bad Request Cache-Control: private Server: Microsoft-IIS/7.5 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Date: Wed, 01 Jul 2020 16:42:35 GMT Content-Length: 0 Any soln?
PHP Curl return 403
I have an url need to get using CURL PHP. When I tried to setup PHP curl as below, I got 403 error $url = "http://r3---sn-ogueln76.googlevideo.com/videoplayback?initcwndbps=22007500&signature=111AEFD82398870EA1643F228D98E7D5834FB654.1CA4909304F857122728B00FC24CD06C751FC181&dur=3812.809&nh=IgpwcjAzLm5ydDE5KgkxMjcuMC4wLjE&sver=3&sparams=dur,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,nh,pl,ratebypass,source,upn,expire&lmt=1460861599174672&mime=video/mp4&ip=45.32.11.179&mm=31&source=youtube&pl=25&mn=sn-ogueln76&mt=1461253133&mv=m&id=o-AHFL53t2W9hKDUR3w8WBd5DOA-1Joopi7CtioNWE1jG4&ms=au&expire=1461274970&key=yt6&upn=zmxYuJKDITA&ratebypass=yes&ipbits=0&itag=18&fexp=9405185,9408350,9416126,9416891,9420452,9422542,9422596,9425077,9426927,9427773,9428398,9430014,9430031,9431012,9431522,9433094,9433097,9433653,9434126,9434765&signature="; $xx = curl_init(); $head[] = "Connection: keep-alive"; $head[] = "Keep-Alive: 300"; $head[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $head[] = "Accept-Language: en-us,en;q=0.5"; curl_setopt($xx, CURLOPT_URL, "$url"); curl_setopt($xx, CURLOPT_BINARYTRANSFER, true); curl_setopt($xx, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36'); curl_setopt($xx, CURLOPT_HEADER, true); curl_setopt($xx, CURLOPT_RETURNTRANSFER, 1); curl_setopt($xx, CURLOPT_TIMEOUT, 60); curl_setopt($xx, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt($xx, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($xx, CURLOPT_HTTPHEADER, $head); curl_setopt($xx, CURLOPT_VERBOSE, true); $verbose = fopen('tmp.log', 'w+'); curl_setopt($xx, CURLOPT_STDERR, $verbose); var_dump(curl_exec($xx)); Here is the error: string(223) "HTTP/1.1 403 Forbidden Last-Modified: Wed, 02 May 2007 10:26:10 GMT Content-Type: text/plain Content-Length: 0 Connection: close X-Content-Type-Options: nosniff Date: Thu, 21 Apr 2016 16:15:25 GMT Server: gvs 1.0 " But when I try to request same url which CURL shell (command prompt). I got 302 code I am suppesting that php curl encoded my request so that the server don't allow and return 403. If you have any idea, please help. Thank you very much.
cURL Login Apple
I'm trying to do a cURL Login to Apple but I don't have any ideea what I have to do in this case. When I run the script I get this error HTTP/1.1 415 Unsupported Media Type X-FRAME-OPTIONS: DENY X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode=block Strict-Transport-Security: max-age=31536000; includeSubDomains Content-Security-Policy: default-src ; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://.apple.com https://.cdn-apple.com; style-src 'unsafe-inline' https://.apple.com https://.cdn-apple.com; connect-src 'self'; img-src 'self' data: https://.apple.com https://.cdn-apple.com https://.icloud.com https://*.mzstatic.com http://appleid-localdev.apple.com:19380; X-BuildVersion: R3 Set-Cookie: aa=6DA3750B50757F6E11A0C57E1173942A124AB682C5CB41FA08B21573379AB051C8A49DD98E9052562E92EA785A36626AAF4D1C966577B7D08DA1B534507112ABCB6298B3C876FB1A67E9F5616C6C77061F5E2396D965A95F1C5D278771E81C6355EDBF0C55CC8DDF9ADA3F992C86952B26669208D66927AC2AAD8B38F09452796EF9A4DFA8190C65ACDA164FE7FDD7DA0D1651F69895728E59FD5DD67FA92C68156D997167B3737A4BC332D0D2A01C9E66C070E02C0B3E6EBF68D0B4794CB3BE83A143B04BD8ED3B73C18DDCC10C0EB4EA65789ADFF11F3AE0E46C38C4BEA8E78EE48A15BB9D1D597B38AF7E9E1D62067871BC882C1EB88EDFE942FAB51C1637; Domain=idmsa.apple.com; Path=/; Secure; HttpOnly Content-Length: 0 Vary: Accept-Encoding Date: Thu, 17 Dec 2015 15:49:29 GMT Server: Apple Set-Cookie: X-SESS=28d4a3dabbd9885122f543812c1be5cd88709143b8732f334e78597f994adfb94b10456f;Version=1;Max-Age=1800;path=/;secure;httponly And this is my code <?php $username = 'mail#mail.com'; $pass = 'password'; $login = array( 'accountName' => $username, 'password' => $pass, 'rememberMe' => 'false' ); $loginUrl = 'https://idmsa.apple.com/appleauth/auth/signin'; $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept-Encoding: gzip', 'Accept: application/json, text/javascript, */*; q=0.01', 'Content-Type: multipart/form-data', 'Connection: Keep-Alive' )); curl_setopt($ch, CURLOPT_ENCODING, "gzip"); curl_setopt($ch, CURLOPT_URL, $loginUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($login)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_REFERER, 'https://idmsa.apple.com/appleauth/auth/signin?widgetKey=af1139274f266b22b68c2a3e7ad932cb3c0bbe854e13a79af78dcc73136882c3&locale=en_EN&rv=1 '); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); $content1 = curl_exec($ch); curl_close($ch); echo $content1; ?> Any suggestions ? Thank you .
TwitterOAuth Parse PHP Curl Response to Get Access Token
I'm using TwitterOAuth to access my timeline and get the last three tweets. I call oauth2/token with Curl and it returns a string which contains the bearer access token. The problem is how do I parse the response to get the access_token? I could use String functions, but it seems there should be a better way. Here is my code: $headers = array( "POST /oauth2/token HTTP/1.1", "Host: api.twitter.com", "User-Agent: my Twitter App v.1", "Authorization: Basic ". $base64_encoded_bearer_token ."", "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Content-Length: 29" ); $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token"); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); $header = curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $curlResponse = curl_exec($curl); curl_close ($curl); This is the $curlResponse variable: string(1018) "HTTP/1.1 200 OK cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0 content-disposition: attachment; filename=json.json content-length: 153 content-type: application/json;charset=utf-8 date: Tue, 21 Apr 2015 14:58:59 GMT expires: Tue, 31 Mar 1981 05:00:00 GMT last-modified: Tue, 21 Apr 2015 14:58:59 GMT ml: S pragma: no-cache server: tsa_a set-cookie: guest_id=v1%3A142962833975637457; Domain=.twitter.com; Path=/; Expires=Thu, 20-Apr-2017 14:58:59 UTC status: 200 OK strict-transport- security: max-age=631138519 x-connection-hash: 128fc5244a58ab23aa3b42c1827d6748 x-content-type-options: nosniff x-frame- options: SAMEORIGIN x-response-time: 10 x-transaction: 073ddea9feba6654 x-tsa-request-body-time: 0 x-twitter-response-tags: BouncerCompliant x-ua-compatible: IE=edge,chrome=1 x-xss-protection: 1; mode=block {"token_type":"bearer","access_token":"AAAAAAAAAAAA AAAAAAAAAGYrfQAAAAAAODa03 sxJgQuYFOyapqOCaSy7mQA%3D IFQZOFEENVMMTosGadfagztbJyZ7bc7ID8wRENK1exJVw48adM6J"}"
You can extract the body from the response by the header size, and then parse it into an object by using json_decode. $headers = array( "POST /oauth2/token HTTP/1.1", "Host: api.twitter.com", "User-Agent: my Twitter App v.1", "Authorization: Basic " . $base64_encoded_bearer_token . "", "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", "Content-Length: 29" ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://api.twitter.com/oauth2/token"); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($curl); $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); curl_close($curl); $header = substr($response, 0, $headerSize); $body = substr($response, $headerSize); var_dump(json_decode($body));
How request xml via PHP-cURL POST request
I want a PHP-cURL script to do the following request. http://site5.way2sms.com/QuickContacts POST /QuickContacts HTTP/1.1 Host: site5.way2sms.com User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive X-Requested-With: XMLHttpRequest Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Referer: http://site5.way2sms.com/Main.action?id=0CD36BD332A5C7AE77FDBA1CBDBFFBB6.w809 Content-Length: 16 gads=ID=e2cdae9b764355fa:T=1333862873:S=ALNI_MYvxochQ56ILMvBDr4oyyqCIDVn3w Pragma: no-cache Cache-Control: no-cache folder=DashBoard HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html Content-Length: 1901 Date: Sun, 08 Apr 2012 06:07:52 GMT Connection: close I think the returned content is in XML format. How to handle them?
If you want generate a post request to the URL and catch the returned data using cURL you can use the following function. function cURL($url, $header=NULL, $cookie=NULL, $p=NULL) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, $header); curl_setopt($ch, CURLOPT_NOBODY, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); if ($p) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $p); } $result = curl_exec($ch); if ($result) { return $result; } else { return curl_error($ch); } curl_close($ch); } Like, $data = cURL("http://site5.way2sms.com/QuickContacts", NULL, NULL, array("post_var" => value, "another_post_var" => val2));