I want to create App Pinterest with PHP Curl but this is not working. The application I follow the POST process: Postman Interceptor
Request:
curl 'https://developers.pinterest.com/apps/'
-H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0'
-H 'Accept: */*'
-H 'Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3' --compressed
-H 'Content-Type: application/json'
-H 'X-CSRFToken: IjExYmYxYzZlZDgwM2RlMGQxNzEyZjdlZTRkZTk3NmQ0OTliZWQwYTki.XlzcOw.zET53I_suQKC2TbplvhpouiuaAM'
-H 'X-Requested-With: XMLHttpRequest'
-H 'Origin: https://developers.pinterest.com'
-H 'DNT: 1'
-H 'Connection: keep-alive'
-H 'Referer: https://developers.pinterest.com/apps/'
-H 'Cookie: _pinterest_sess=TWc9PSZESDJWQ0dCNmN0TERrZThaSEFLMnhrekhLVVRzaVMwKzBxY3YxaTJmMDdZMS9kR1V4SHB0UVorajRyY21TU1A4TCtXUElEdlpDMGVNd3BCcGhFYXg0Q3JjTm1uK2VxQ2dMTlo1UHJoQ2ZXc2t6YytLV01VYTNqNU5pL0tqQlZtWU9EMjQ0NGh4UG95VC93b1BPM21kRzNWSHlQT0tpaEdIZ2xNWmd1WUlvdUFDaHBrZDVLRHdYdWVUbnN6cEtRV1QrU1pUdzNTbElaZ0Y3VVc5VW81c1ArQzRZam41dEFCTVV6dEFCMngySnZ6dDZWeUNkeXA3cW5ZcGZUZkFGL1o4TjBnMFlCYUUzaEtEL0VwOTZIdkVHWXpQNjJ5NHRZUm5YNk5YU1hWWWgwV0FCWVJHbzFEb2Z3U24zR0FrbzZTcVJPaXZFYzduTCtCc3RPejYrVFFFd2pDOXM0czRpby96V3lzQlpFVzBLdEk1REVDVk0rajgxeGZRNldXUE9sc24xak50S3pPV3BzdWFXdkhEZ3Q5OUhiYlVjNHBPeUNsYkoySVpRMFpnd2QrcjNtNW0waUhZWkk4YjlXakFiTEtTaGJUOXpiRE9CWDJxcEUzLzZWYVNhZ1NmRDJFWTFDenhJZkZVdmoyeGI1WFZ6TldpZWFWeVhDNFozNkFpcEFsM0k3YlZUMkZiTEROZHpoNnFEdjdpdmJxMW14dVFSWnlETkRTUGFrWDNOd1MycUdETGZWYWNMT0JkbEVxS2N4TmhQekZiaE5zSkltWkVtQTZ6STV0Nm5yMzg2R2toREdiTHRPZ0ZlRXVPc1V2R3hIYUMrNjdNWGFOOGcxL1VHOStNWmZBRHFXd3JOZFJhRFVheXY2emVuRUszZVhuZFRkemdMS3hiL05aajd5WFYyMk5YQWR6MmcvYU9Pa1BtUnU1MTVZQjVmbDZyQjkyMDJXSGh1dXBjamFINGhwU2tYZXpRN1Z1UE1zbU83bytadkFnRURQSy9XcEF3d3VodDh5MXcwTU1LblFGbDUwdWk0a1VleC8vQTRlV3lzejNyeWpRc2sralV6dFZ3c0lMYjZjWFNRMTZreVh2R0Q4OHlSWG91JkE3eERJQXpWS1YyUG1GcldkZHhFeXpCdDlyST0=; _auth=1; session=eyJjc3JmX3Rva2VuIjoiMTFiZjFjNmVkODAzZGUwZDE3MTJmN2VlNGRlOTc2ZDQ5OWJlZDBhOSJ9.XlzdMQ.qu4RJUJgbv0UygBZFyPjw6-aGkw; csrftoken=lwBLncs70zm1Y2Xhx9D7M6w6pVZQw6ro; _dev_sess=TWc9PSZXMDN0NjdEK3FQSWw3WFFUaUNHSEdZVFdLYVgxME9WUkZDTlU1UGczUXRUeWpNZEVrUmZIbjFQSFlXK2V1NTBaS3JrNFZCaW1FVnY3WFRhODNVT3pwQ3ljUldQUXFmU1JFcVNuc2orNi9jSGpnRFAwVjlhRDdJUnBRekNGb3FZK21idk9IdFdlVXpJVzJkZnptaGZpRUtSQ2xpTnZPU2ZZMVdNNFY5MEM5cXV5ZUlzelYrM0tEL1JodU9yM3BBaUw2KzV2WEdJa3RJeEJQdTJSRjYrcUlIakFTS3BXRS9aNldUOXM1dmVLK2VYT1I5TENYQ0hSNGpzd21HcE9icU95M3poSVB4RDVyT1BQeUdnUnhjaGdVTFlvNHR4cUl6Zk9uZllmL1g5NEVZRDgzSzNxWjJPZFJjZXdQaVhuWnBkQzFhVUQzMm42NFplb2k5VUFMWTM3bnc9PSZEd0Y2VUlRQVlvOS9NbmMzcFBmaFNzdlQ4cGs9; country-code=TR'
-H 'TE: Trailers' --data '{"name":"test1","description":"test description"}'
Full Codes:
<?php
//preg_match('/csrftoken(.*?)[\b;\s]/i', $headers, $csrf_token);
error_reporting(E_ALL);
ini_set('display_errors', 1);
$appname = "deneme33";
$appdescription = "lorem ipsum deneme";
$username = "talithae.matson_53#hotmail.com";
$password = "SahsZGHx";
// this is the http post data for logging in - username & password are substituted in later
$login_post = array(
'source_url' => '/login/',
'data' => '{"options":{"username_or_email":"%s","password":"%s"},"context":{}}',
'module_path' => 'App()>LoginPage()>Login()>Button(class_name=primary, text=Log In, type=submit, size=large',
);
$create_app = array(
'data' => '{"name":"%s","description":"%s"}',
);
$pinterest_url = 'https://www.pinterest.com/'; //'https://www.pinterest.com/'; // pinterest home url
$developer_url = 'https://developers.pinterest.com/apps/'; // Developer home url
$login_url = $pinterest_url . 'login/'; // pinterest login page url
$login_post_url = $pinterest_url . 'resource/UserSessionResource/create/'; // pinterest login post url
// http headers to send with requests
$httpheaders = array(
'Connection: keep-alive',
'Pragma: no-cache',
'Cache-Control: no-cache',
'Accept-Language: en-US,en;q=0.5',
);
// http headers to send when logging in
$login_header = array(
'X-NEW-APP: 1',
'X-APP-VERSION: 08930e3', // THIS WILL UPDATE FREQUENTLY, CHANGE IT!!!
'X-Requested-With: XMLHttpRequest',
'Accept: application/json, text/javascript, */*; q=0.01');
$developer_header = array(
'Accept: */*',
'Sec-Fetch-Dest: empty',
'X-Requested-With: XMLHttpRequest',
'Content-Type: application/json');
// ----------------------------------------------------------------------------
// request home page to establish cookies and a session, set curl options
$ch = curl_init($pinterest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, fopen('debug.txt', 'w+'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch);
sleep(5);
//echo "Giriş Datası".$data;
// ----------------------------------------------------------------------------
// parse the csrf token out of the cookies to set later when logging in
list($headers, $body) = explode("\r\n\r\n", $data, 2);
$headers = file_get_contents('cookie.txt', true);
//print_r($headers);
preg_match("/csrftoken+(.*?)\n/i",$headers,$matches);
$csrf_token=trim($matches[1]);
preg_match("/session+(.*?)\n/i",$headers,$matches);
$session=trim($matches[1]);
echo "Token:". $csrf_token.PHP_EOL;
echo "Session:". $session.PHP_EOL;
//die();
// next request the login page
curl_setopt($ch, CURLOPT_URL, $login_url);
$data = curl_exec($ch);
//Developer Cookie Get
$ch = curl_init($developer_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, fopen('debug.txt', 'w+'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch);
//Developer Cookie Get
//Make APP
$create_app['data'] = sprintf($create_app['data'], $appname, $appdescription);
$post = http_build_query($create_app);
print_r($post);
die();
curl_setopt($ch, CURLOPT_URL, $developer_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: */*",
"Sec-Fetch-Dest: empty",
"X-CSRFToken: ".$csrf_token,
"X-Requested-With: XMLHttpRequest",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
"Content-Type: application/json",
"Cache-Control: no-cache",
"Host: developers.pinterest.com",
"Accept-Encoding: gzip, deflate, br",
"Cookie: session=".$session,
"Connection: keep-alive"
));
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); // Cookie aware
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); // Cookie aware
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
//Make APP
//echo "Baş Header".print_r($developer_header);
echo $data;
die();
$login_header[] = 'X-CSRFToken: ' . $csrf_token;
$login_post['data'] = sprintf($login_post['data'], $username, $password);
$post = http_build_query($login_post);
curl_setopt($ch, CURLOPT_URL, $login_post_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge($httpheaders, $login_header));
curl_setopt($ch, CURLOPT_REFERER, $login_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
echo $data;
// ----------------------------------------------------------------------------
// perform login post
//print_r($csrf_token);
// check response and output status
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
echo "Error logging in.<br />";
var_dump(curl_getinfo($ch));
} else {
$response = json_decode($data, true);
if ($response === null) {
echo "Failed to decode JSON response.<br /><br />";
var_dump($response);
} else if ($response['resource_response']['status'] === "success") {
print_r($response['resource_response']);
//print_r($response['resource_response']['data']);
//print_r($response['resource_response']['username']);
echo "Login successful, " . $response['resource_response']['user']['username'] . "<br /><br />";
echo "You have {$response['resource_response']['data']['follower_count']} followers, are following {$response['resource_response']['data']['following_count']} users. You have liked {$response['resource_response']['data']['like_count']} pins.";
}
}
I think there is a problem with the data I sent after //Make APP.
Cookies: https://i.imgur.com/OoyVh4C.jpg
Body: https://i.imgur.com/A02mfGe.jpg
Headers: https://i.imgur.com/Adc8Dmd.jpg
Result: https://i.imgur.com/sXBPQxM.jpg
I might be missing something during the post operation.
What am I missing in xhr post sending?
The URL you are trying to call is not part of their normal REST API it looks like. I believe you are supposed to visit that manually and create your app first. Then work with their API after that.
Source
Did you find documentation from them that allows you to do that, or are you trying to hack a method together to accomplish it?
Related
$username = "login";
$password = "pass";
$cookie = $username.".txt";
$useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36";
$url = 'https://instagram.com/';
$arrSetHeaders = array(
"User-Agent: $useragent",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
'Connection: keep-alive',
'cache-control: max-age=0'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
sleep(5);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches);
$cookieFileContent = '';
foreach($matches[1] as $item)
{
$cookieFileContent .= "$item; ";
}
$cookieFileContent = rtrim($cookieFileContent, '; ');
$cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent);
$oldContent = file_get_contents(dirname(__FILE__)."/".$cookie);
$oldContArr = explode("\n", $oldContent);
if(count($oldContArr)) {
foreach($oldContArr as $k => $line){
if(strstr($line, '# ')){
unset($oldContArr[$k]);
}
}
$newContent = implode("\n", $oldContArr);
$newContent = trim($newContent, "\n");
file_put_contents( dirname(__FILE__)."/".$cookie, $newContent);
}
preg_match('|csrftoken(.*)|', file_get_contents(dirname(__FILE__)."/".$cookie), $csrf);
$crf = trim($csrf[1]);
$arrSetHeaders = array(
"User-Agent: $useragent",
"X-CSRFToken: $crf",
"x-csrftoken: $crf",
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest',
"Referer: https://instagram.com",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
'Connection: keep-alive',
'cache-control: max-age=0',
);
$post = array('username' => $username, 'password' => $password, 'csrfmiddlewaretoken' => $crf);
$url = 'https://instagram.com/accounts/login/ajax/';
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, 'https://instagram.com/');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
$page = curl_exec($ch);
curl_close($ch);
var_dump($page);
returns bool(false)
what is wrong? how to log in instagram using curl request to accounts/login/ajax/
UPDATE:
I'm able to do a request in this way, i'm using curl.
I don't use any sort of cookie. Just the straight request
curl -H "Content-Type: application/x-www-form-urlencoded"
-H "Accept: */*"
-H "Accept-Encoding: gzip, deflate, br"
-H "Host: www.instagram.com"
-H "Origin: https://www.instagram.com"
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15"
-H "Referer: https://www.instagram.com/accounts/login/"
-H "Connection: keep-alive"
-H "X-Requested-With: XMLHttpRequest"
-H "X-IG-WWW-Claim: 0"
-H "X-CSRFToken: $token"
--data "username=$us&enc_password=%23PWD_INSTAGRAM_BROWSER%3A0%3A$rand%3A$line&queryParams=%7B%7D&optIntoOneTap=false" --compressed "https://www.instagram.com/accounts/login/ajax/"
^^^ ^^^^^ ^^^^^
$token is a 32 LENGTH ALPHANUMERIC STRING
Example: ZHMFltOnFJZMPdXyFdCCTsJrzEfSmGCg
$us is the username in PLAIN ASCII TEXT
Example: myaccount
$rand is a 10 LENGTH DIGIT STRING
Example: 9719014829
$line is the password in PLAIN ASCII TEXT
Example: mypassword
There are 3 possibile responses if the username is correct
authenticated:true -> Password was ok
authenticated:false -> Password not ok
checkpoint_required -> Password ok but request was blocked, the user has 2FA or
the ip is blacklisted
:)
I need to access this URL on php:
https://wmf.ok.ru/play;jsessionid=a-pt2O8FJKq_wzqod9LAJNtwgjNSjaNa-KVIGc1d1eRUSWhdAw9dlDo13fLzh57rGyKPzk2V0jMFrnKw8R4HjA.p162X6pZ_FG0kKMmKa6bkQ?client=flash&jsonp=&tid=40542951634095&ctx=my
But on my PHP code I got 404 error. I have done everything correctly. I think there is a mistake with ; symbol. We can open the link above on chrome, but not on php curl. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Encoding: gzip, deflate, br',
'Accept-Language: en-US,en;q=0.9,az;q=0.8,tr;q=0.7,uz;q=0.6,ru;q=0.5',
'Referer: https://ok.ru/',
'Origin: https://ok.rus'
));
$data = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $data;
}
$url = 'https://wmf.ok.ru/play;jsessionid=a-pt2O8FJKq_wzqod9LAJNtwgjNSjaNa-KVIGc1d1eRUSWhdAw9dlDo13fLzh57rGyKPzk2V0jMFrnKw8R4HjA.p162X6pZ_FG0kKMmKa6bkQ?client=flash&jsonp=&tid=40542951634095&ctx=my';
echo file_get_contents_curl($url);
?>
After executing this code, I got microsoft's server 404 error. How can I make Curl to open URLs like this?
Just add this to your function and it will work:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
Here is the full working function:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Encoding: gzip, deflate, br',
'Accept-Language: en-US,en;q=0.9,az;q=0.8,tr;q=0.7,uz;q=0.6,ru;q=0.5',
'Referer: https://ok.ru/',
'Origin: https://ok.rus'
));
$data = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
echo curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
curl_close($ch);
return $data;
}
$url = 'https://wmf.ok.ru/play;jsessionid=a-pt2O8FJKq_wzqod9LAJNtwgjNSjaNa-KVIGc1d1eRUSWhdAw9dlDo13fLzh57rGyKPzk2V0jMFrnKw8R4HjA.p162X6pZ_FG0kKMmKa6bkQ?client=flash&jsonp=&tid=40542951634095&ctx=my';
echo file_get_contents_curl($url);
?>
I have a problem with curl. I need to extract data from my profile with website (validity of the account). The problem is the lack of logging.
$login = "user";
$password = "passtest";
$ckfile = tempnam("./cookies", "cookies.txt");
$page_login = "http://host.tv/login.html";
$page_download = "http://host.tv/profil.html";
$post_data = "?login=$login&pass=$password";
$agent = "Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3";
$headers = array();
$headers[] = 'GET /login.html HTTP/1.1';
$headers[] = 'Host: host.tv';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8';
$headers[] = 'Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4';
$headers[] = 'Referer: http://host.tv/login.html';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36';
$connect = curl_init ();
curl_setopt($connect, CURLOPT_URL, $page_login);
curl_setopt($connect, CURLOPT_COOKIESESSION, 1);
curl_setopt($connect, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($connect, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($connect, CURLOPT_TIMEOUT, 40);
curl_setopt($connect, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($connect, CURLOPT_HEADER, 0);
curl_setopt($connect, CURLOPT_HTTPHEADER, $headers);
curl_setopt($connect, CURLOPT_USERAGENT, $agent);
curl_setopt($connect, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($connect, CURLOPT_POST, 1);
curl_setopt($connect, CURLOPT_POSTFIELDS, $post_data);
curl_exec($connect);
curl_setopt($connect, CURLOPT_URL, $page_download);
$page = curl_exec($connect);
curl_close($connect);
echo $page;
The script does not logged do Accounts.
you are doing some mistakes here,
1: don't manually create the GET / header, let curl create it for you with CURLOPT_URL / CURLOPT_HTTPGET
2: logging in is not a GET operation, its a POST operation.
and there's a lack of error checking on the curl return values.
here's an example of logging in, going to the profile page, and extract the account type (i guess it says "free account" or something, but i dont speak that language), using hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php
<?php
declare(strict_types=1);
require_once('hhb_.inc.php');
hhb_init();
$hc=new hhb_curl();
$hc->_setComfortableOptions();
//getting a session
$hc->exec('http://host.tv/login.html');
//logging in
$username='user';
$password='passtest';
$hc->setopt_array(array(
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query(array(
'login'=>$username,
'pass'=>$password,
'logged'=>''//??
))
));
$hc->exec('http://host.tv/login.html');
//TODO: confirm login was successful
$hc->exec('http://host.tv/profil.html');
//extract "account type" from your profile
$domd=#DOMDocument::loadHTML($hc->getResponseBody());
$accountType=trim(preg_replace('/\s+/', "\n", $domd->getElementById("content-top-right")->textContent));
echo $accountType.PHP_EOL;
output:
Konto:
Standard
Ważne
do:
Brak
I need to get the source code of a page that is only accessible when I am logged in. However, when I use the following code, it doesn't recognize that I'm logged in, and asks me to re-login.
$opts = array('http' => array('header'=> 'Cookie: ' . $COOKIEFILE."\r\n"));
$context = stream_context_create($opts);
$file = file_get_contents('http://www.example.com/', false, $context);
echo $file;
I have researched and found examples similar to mine, but they don't help me. When I use the above code it doesn't recognize my cookies.
Full code:
$USERNAME = 'myEmail';
$PASSWORD = 'myPassword';
$COOKIEFILE = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_URL,
'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage');
$data = curl_exec($ch);
$formFields = getFormFields($data);
$formFields['Email'] = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
$post_string = '';
foreach($formFields as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string, 0, -1);
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
$ch = curl_init ("http:/example.com");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $COOKIEFILE);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
var_dump($output);
Allen, in situations like yours, when I'm having trouble login with curl, what I normally do is use Live Http Headers for firefox to log my requests and then use the request headers with curl CURLOPT_HTTPHEADER, the cookies will be included in the headers.
Flow:
Log in to the website using your browser, once logged, log any request with Live Http Headers for firefox, copy and paste the request to an array, something like :
$myHeaders = array(
"Host: stackoverflow.com",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0",
"Accept: application/json, text/javascript, */*; q=0.01",
"Accept-Language: en-US,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"DNT: 1",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With: XMLHttpRequest",
"Referer: http://stackoverflow.com/questions/32933636/use-cookie-with-curl-php",
"Content-Length: 482",
"Cookie: mycookie :)",
"Connection: keep-alive",
"Pragma: no-cache"
);
Then, use the headers with curl:
curl_setopt($ch, CURLOPT_HTTPHEADER, $myHeaders );
Notes:
1- No need for CURLOPT_COOKIEJAR or CURLOPT_COOKIEFILE
2- This works with almost every site I've tried.
3- Some cookies may expire after a while, others, like google, never expire.
I got a problem with downloading video from videoslasher.com. I wrote a script to get source link and it is working fine.
Example link:
http://storage2.videoslasher.com/free/K/K1/K1Z6TBW88QCM.flv?h=XeahiwUFe_yHiduYx5EzBg&e=1377200810
But when i am downloading with script i get 403 (forbidden).
Code downloading video:
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: pl,en-us;q=0.7,en;q=0.3';
$headers[] = 'Accept-Encoding: gzip, deflate';
$headers[] = 'Connection: keep-alive';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.videoslasher.com/static/player/flowplayer.commercial-3.2.7.swf');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096000);
echo curl_exec($ch);
curl_close($ch);
Does anyone know what could be the problem?