TwitterOAuth Parse PHP Curl Response to Get Access Token - php

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

Related

Curl/php request moving to localhost after second request

I'm trying to get a json result from a website to analyze in my system.
In the first request of the day I receive Status 200 OK I get the result that I'm expecting.
But if I try to get the result again making a refresh in the page for example, I receive the following error
"close Cache-Control: no-cache Pragma: no-cache "
With the following HTTP HEADER
"HTTP/1.1 302 Found : Moved Temporarily Location: http://localhost Cache-Control: private, no-cache, max-age=0, no-store, must-revalidate Expires: Thu, 01 Jan 1970 00:00:01 +0000 Pragma: no-cache
For some reason the server returns error 302 and move to localhost and I have to wait until the next day to get the result 200 again.
Running var_dump(curl_error($curl));. I get bool(false)
My code
<?php $cookie="files/cookies.txt";
$url = "https://extra.bet365.com/ResultsApi/GetResults?sportName=sport&sportId=146&fixtureId=104716787&competitionId=20700663&fromDate=2021-07-01T04:05:00&toDate=2021-07-01T04:05:00&challengeId=62233643&marketOverride=";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookie);
$headers = array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:89.0) Gecko/20100101 Firefox/89.0",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3",
"Connection: keep-alive",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp); ?>

PHP : all cookies are not saved with cURL

I'm running following code :
$username = 'alex';
$loginUrl = 'https://mysite/login';
$ckfile = '/home/alex/www/storage/logs/cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username);
curl_setopt($ch, CURLOPT_COOKIEJAR,$ckfile);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$status_code = curl_getinfo($ch);
$resp = curl_exec($ch);
curl_close($ch);
by running return $resp i get:
HTTP/1.1 302 Found Date: Sat, 26 Nov 2016 07:08:59 GMT Expires: Mon, 02 Aug 1999 00:00:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Connection: close Set-Cookie: mnproxy=tEiyCofS5YEOiFe; Path=/; Domain=.mysite Location: http://mysite/connect?session=stEiyCofS5YEOiFe&url=menu Vary: Accept-Encoding Content-Length: 352 Content-Type: text/html; charset=iso-8859-1 Set-Cookie: TS0110ac54=01d9c2a5b1932847d951b9185a227fd3d4bfdf23358732abbed5a18dc8b027fc893bda0c4bf3720f253354b709cb68a505ec9cac6dc051e3e792ddf322a734b650b6c33f2e; Path=/; Domain=.mysite
and the cookie.txt :
.auth.mysite TRUE / FALSE 0 TS0110ac54 01d9c2a5b1932847d951b9185a227fd3d4bfdf23358732abbed5a18dc8b027fc893bda0c4bf3720f253354b709cb68a505ec9cac6dc051e3e792ddf322a734b650b6c33f2e
Here you can see none of headers are saved in cookie else TS0110ac54.
what am i doing wrong?

cURL + PHP + File Upload issues

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?

PHP cURL refuses to POST to website

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.

PHP cURL POST bad output

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;
}

Categories