I want to scrape some LinkedIn company pages with cURL and PHP with login Credentials. I tried this code. But I got error like
Unauthorized
You must be authenticated to access this page.
Before scraping the company page I have to sign in at LinkedIn with a personal account via cURL, but it doesn't seems to work.
Instead of using simple_html_dom we used above fetch_value.
function fetch_value($str, $find_start = '', $find_end = '') {
if ($find_start == '') {
return '';
}
$start = strpos($str, $find_start);
if ($start === false) {
return '';
}
$length = strlen($find_start);
$substr = substr($str, $start + $length);
if ($find_end == '') {
return $substr;
}
$end = strpos($substr, $find_end);
if ($end === false) {
return $substr;
}
return substr($substr, 0, $end);
}
$linkedin_login_page = "https://www.linkedin.com/uas/login";
$linkedin_ref = "https://www.linkedin.com";
$username = 'username';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $linkedin_login_page);
curl_setopt($ch, CURLOPT_REFERER, $linkedin_ref);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7)');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$login_content = curl_exec($ch);
if (curl_error($ch)) {
echo 'error:' . curl_error($ch);
}
$var = array(
'isJsEnabled' => 'false',
'source_app' => '',
'clickedSuggestion' => 'false',
'session_key' => trim($username),
'session_password' => trim($password),
'signin' => 'Sign In',
'session_redirect' => '',
'trk' => '',
'fromEmail' => ''
);
$var['loginCsrfParam'] = fetch_value($login_content, 'type="hidden" name="loginCsrfParam" value="', '"');
$var['csrfToken'] = fetch_value($login_content, 'type="hidden" name="csrfToken" value="', '"');
$var['sourceAlias'] = fetch_value($login_content, 'input type="hidden" name="sourceAlias" value="', '"');
$post_array = array();
foreach ($var as $key => $value) {
$post_array[] = urlencode($key) . '=' . urlencode($value);
}
$post_string = implode('&', $post_array);
curl_setopt($ch, CURLOPT_URL, "https://www.linkedin.com/uas/login-submit");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$store = curl_exec($ch);
if (stripos($store, "session_password-login-error") !== false) {
$err = trim(strip_tags(fetch_value($store, '<span class="error" id="session_password-login-error">', '</span>')));
echo "Login error : ".$err;
} elseif (stripos($store, 'profile-nav-item') !== false) {
curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/company-beta/10667/?pathWildcard=10667');
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$content = curl_exec($ch);
curl_close($ch);
echo $content;
} else {
echo "unknown error";
}
Any suggestion please help?
Thanks!
Related
I have spend much of time on it, but did not found any working solution ...
I have tried the following code .. but always else case is running "didnt find login form1"
I have tried another coders11 inplemented api but it was also deprecated...
I found many other solutions but not in php ... I am looking for solution in php...
class googleAlerts{
public function createAlert($alert){
$USERNAME = 'XXXXXX#gmail.com';
$PASSWORD = 'YYYYYY';
$COOKIEFILE = 'cookies.txt';
$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 = $this->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);
if (strpos($result, '<title>') === false) {
return false;
} else {
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, null);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/create');
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
//var_dump($result);
$result = $this->getFormFieldsCreate($result);
$result['q'] = $alert;
$result['t'] = '7';
$result['f'] = '1';
$result['l'] = '0';
$result['e'] = 'feed';
unset($result['PersistentCookie']);
$post_string = '';
foreach($result as $key => $value) {
$post_string .= $key . '=' . urlencode($value) . '&';
}
$post_string = substr($post_string, 0, -1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com/alerts/manage');
$result = curl_exec($ch);
if (preg_match_all('%'.$alert.'(?=</a>).*?<a href=[\'"]http://www.google.com/alerts/feeds/([^\'"]+)%i', $result, $matches)) {
return ('http://www.google.com/alerts/feeds/'.$matches[1][0]);
} else {
return false;
}
}
}
private function getFormFields($data)
{
if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
private function getFormFieldsCreate($data)
{
if (preg_match('/(<form.*?name=.?.*?<\/form>)/is', $data, $matches)) {
$inputs = $this->getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form1');
}
}
private function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
}
$alert = new googleAlerts;
echo $alert->createAlert('YOUR ALERT');```
You can't login into google alerts with password and email anymore, you would have to pre-create cookies by login into google alerts and copying them out of the dev console and then passing them as argument when doing a curl request. Check out my google alerts api i have written in php. Maybe that helps you out https://github.com/Trivo25/google-alerts-api-php
I have a form which uses curl to submit the apiKey to my server and then the script on my server verify the key and returns true and false. but instead of response. I'm getting Trying to access array offset on value of type null. I want to know How to get response from my server after curl submission.
Curl Submit
$post['apiKey'] = $apiKey;
$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_URL,"https://www.pawnhost.com/phevapi/verify_api.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$json = curl_exec($ch);
$response = json_decode($json, true);
Server Script
<?php
define("ERROR_HEADER_URL", "Location: " . $_SERVER['HTTP_REFERER'] . "?error=");
require("includes/initialize.php");
if ($_SERVER['REQUEST_METHOD'] != 'POST') header(ERROR_HEADER_URL . "invalidRequest");
$postParams = allowedPOSTParams($allowed_params=['apiKey']);
if (!isset($postParams['apiKey'])) header(ERROR_HEADER_URL . "verficationFailed");
$apiKey = escape($postParams['apiKey']);
if (isInputEmpty($apiKey)) {
header(ERROR_HEADER_URL . "emptyFields");
} elseif (!$apiKey == 25) {
header(ERROR_HEADER_URL . urlencode("invalidKey"));
} else {
$response = [];
if (getApiKeyUserDetails($apiKey, $connection)) {
if (getApiKeyUserDetails($apiKey, $connection)['apiKeyUsed'] > 0) {
$response['success'] = false;
$response['error'] = 'apiKeyUsed';
} else {
makeApiKeyUsed($apiKey, $connection);
$response['success'] = true;
}
} else {
$response['success'] = false;
$response['error'] = 'invalidApiKey';
}
return json_encode($response);
}
Allowed Post Params Function:
function allowedPOSTParams($allowed_params=[]) {
$allowed_array = [];
foreach ($allowed_params as $param) {
if (isset($_POST[$param])) {
$allowed_array[$param] = $_POST[$param];
} else {
$allowed_array[$param] = NULL;
}
}
return $allowed_array;
}
Replace
curl_setopt($ch, CURLOPT_POSTFIELDS, $apiKey);
with
curl_setopt($ch, CURLOPT_POSTFIELDS, array('apiKey'=>$apiKey));
Then, you will able to find apiKey as POST parameter.
I have a PHP Simple HTML DOM Parser locally in MAMP that pulls information and works well with the Japan version of a website, since I'm located in Japan. However, I would like to pull information from the UK version of the site. What is the simplest way to do this?
I tried the following from the documentation and it didn't work.
$context = array('http' => array('proxy' => '212.82.126.32:80','request_fulluri' => true,),);
$stream = stream_context_create($context);
$html = file_get_html('http://www.supremenewyork.com/shop/new', false, $stream);
I also tried the curl version with modifications as the site has safe mode enabled. That didn't work as well.
function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) {
$mr = $maxredirect === null ? 5 : intval($maxredirect);
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, $mr);
} else {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
if ($mr > 0) {
$newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$rch = curl_copy_handle($ch);
curl_setopt($rch, CURLOPT_HEADER, true);
curl_setopt($rch, CURLOPT_NOBODY, true);
curl_setopt($rch, CURLOPT_FORBID_REUSE, false);
curl_setopt($rch, CURLOPT_RETURNTRANSFER, true);
do {
curl_setopt($rch, CURLOPT_URL, $newurl);
$header = curl_exec($rch);
if (curl_errno($rch)) {
$code = 0;
} else {
$code = curl_getinfo($rch, CURLINFO_HTTP_CODE);
if ($code == 301 || $code == 302) {
preg_match('/Location:(.*?)\n/', $header, $matches);
$newurl = trim(array_pop($matches));
} else {
$code = 0;
}
}
} while ($code && --$mr);
curl_close($rch);
if (!$mr) {
if ($maxredirect === null) {
trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING);
} else {
$maxredirect = 0;
}
return false;
}
curl_setopt($ch, CURLOPT_URL, $newurl);
}
}
return curl_exec($ch);
}
$url = 'http://www.supremenewyork.com/shop/new';
$proxy = '212.82.126.32:80';
$options = array(
CURLOPT_PROXY => $proxy,
CURLOPT_HTTPPROXYTUNNEL => 0,
CURLOPT_REFERER => "http://www.google.com",
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1",
CURLOPT_CONNECTTIMEOUT => 20,
CURLOPT_TIMEOUT => 20,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HEADER => true,
);
$ch = curl_init( $url );
//curl_setopt_array( $ch, $options );
$content = curl_exec_follow( $ch );
$html = new simple_html_dom();
$html->load($content,true,false);
I tried uploading to US and UK servers as well, but that didn't work and it just pulls US data. Some help please?
Curl works whatever safe mode is enable or disable.
Your Curl script is too complex, make it simple and try again.
$content = curl_exec_follow('http://www.supremenewyork.com/shop/new');
$html = new simple_html_dom();
$html->load($content,true,false);
I modified your code, you can try.
// define cookie file path here
define('CRAWLER_COOKIE_FILENAME', 'cookie.txt');
function curl_exec_follow($url) {
$proxy = '212.82.126.32:80';
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1';
// Some websites check referrer
$host = parse_url($url, PHP_URL_HOST);
$scheme = parse_url($url, PHP_URL_SCHEME);
$referrer = $scheme . '://' . $host;
$ch = curl_init();
$curl_defaults = array(
CURLOPT_HEADER => 0,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
);
curl_setopt_array($ch, $curl_defaults);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $referrer);
if ( !file_exists(CRAWLER_COOKIE_FILENAME) || !is_writable(CRAWLER_COOKIE_FILENAME) ) {
echo 'Cookie file is missing or not writable.';
exit;
}
curl_setopt($ch, CURLOPT_COOKIESESSION, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, CRAWLER_COOKIE_FILENAME);
curl_setopt($ch, CURLOPT_COOKIEJAR, CRAWLER_COOKIE_FILENAME);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
// allow to crawl https webpages
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
// the download speed must be at least 1 byte per second
curl_setopt($ch,CURLOPT_LOW_SPEED_LIMIT, 1);
// if the download speed is below 1 byte per second for more than 30 seconds curl will give up
curl_setopt($ch,CURLOPT_LOW_SPEED_TIME, 30);
$content = curl_exec($ch);
if ($ret === FALSE) {
echo curl_error($ch);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ( $code != '200' ) echo 'http error code: ' . $code;
curl_close($ch);
return $content;
}
I would like to integrate way2sms api in php code.
here my sample code , its error free but no message is sending . friends please help me where i have did mistake ? what do i need to change ?
sms.php
$uid='9876543210';//10 digit mobile number
$pwd='password';
$phone='9876543210';
$msg='from way 2 sms master ' ;
include ('way2sms-api.php');
$res= sendWay2SMS ( $uid , $pwd , $phone , $msg);
way2sms-api.php
<?php
function sendWay2SMS($uid, $pwd, $phone, $msg)
{
$curl = curl_init();
$timeout = 30;
$result = array();
$uid = urlencode($uid);
$pwd = urlencode($pwd);
$autobalancer = rand(1, 8);
curl_setopt($curl, CURLOPT_URL, http://site".$autobalancer.".way2sms.com/Login1.action");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "username=".$uid."&password=".$pwd."&button=Login");
//curl_setopt($curl , CURLOPT_PROXY , '144.16.192.218:8080' );
curl_setopt($curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_MAXREDIRS, 20);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER, "http://site".$autobalancer.".way2sms.com/");
$text = curl_exec($curl);
// Check if any error occured
if (curl_errno($curl))
return "access error : ". curl_error($curl);
// Check for proper login
$pos = stripos(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL), "Main.action");
if ($pos === "FALSE" || $pos == 0 || $pos == "")
return "invalid login";
if (trim($msg) == "" || strlen($msg) == 0)
return "invalid message";
$msg = urlencode(substr($msg, 0, 160));
$pharr = explode(",", $phone);
$refurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
curl_setopt($curl, CURLOPT_REFERER, $refurl);
curl_setopt($curl, CURLOPT_URL,
"http://site".$autobalancer.".way2sms.com/jsp/InstantSMS.jsp");
$text = curl_exec($curl);
preg_match_all('/<input[\s]*type="hidden"[\s]*name="Action"[\s]*id="Action"[\s]*value="?([^>]*)?"/si', $text, $match);
$action = $match[1][0]; // get custid from the form fro the Action field in the post form
foreach ($pharr as $p)
{
if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false)
{
$result[] = array('phone' => $p, 'msg' => urldecode($msg), 'result' => "invalid number");
continue;
}
$p = urlencode($p);
// Send SMS
curl_setopt($curl, CURLOPT_URL, 'http://site'.$autobalancer.'.way2sms.com/quicksms.action');
curl_setopt($curl, CURLOPT_REFERER, curl_getinfo($curl, CURLINFO_EFFECTIVE_URL));
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,
"HiddenAction=instantsms&bulidgpwd=*******&bulidguid=username&catnamedis=Birthday&chkall=on&gpwd1=*******&guid1=username&ypwd1=*******&yuid1=username&Action=".
$action."&MobNo=".$p."&textArea=".$msg);
$contents = curl_exec($curl);
//Check Message Status
//preg_match_all('/<span class="style1">?([^>]*)?<\/span>/si', $contents, $match);
//$out=str_replace(" ","",$match[1][0]);
$pos = strpos($contents, 'Message has been submitted successfully');
$res = ($pos !== false) ? true : false;
$result[] = array('phone' => $p, 'msg' => urldecode($msg), 'result' => $res);
}
//echo $text;
// Logout
curl_setopt($curl, CURLOPT_URL, "http://site".$autobalancer.".way2sms.com/LogOut");
curl_setopt($curl, CURLOPT_REFERER, $refurl);
$text = curl_exec($curl);
curl_close($curl);
return $result;
}
?>
TRY THIS CODE:
<?php
/**
* https://github.com/kingster/Way2SMS-API/blob/master/way2sms-api.php
*
* Please use this code on your own risk. The author is no way responsible for the outcome arising out of this
* Good Luck!
**/
class WAY2SMSClient
{
var $curl;
var $timeout = 30;
var $jsToken;
var $way2smsHost;
var $refurl;
/**
* #param $username
* #param $password
* #return bool|string
*/
function login($username, $password)
{
$this->curl = curl_init();
$uid = urlencode($username);
$pwd = urlencode($password);
// Go where the server takes you :P
curl_setopt($this->curl, CURLOPT_URL, "http://way2sms.com");
curl_setopt($this->curl, CURLOPT_HEADER, true);
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($this->curl);
if (preg_match('#Location: (.*)#', $a, $r))
$this->way2smsHost = trim($r[1]);
// Setup for login
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "Login1.action");
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "username=" . $uid . "&password=" . $pwd . "&button=Login");
curl_setopt($this->curl, CURLOPT_COOKIESESSION, 1);
curl_setopt($this->curl, CURLOPT_COOKIEFILE, "cookie_way2sms");
curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->curl, CURLOPT_MAXREDIRS, 20);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($this->curl, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($this->curl, CURLOPT_REFERER, $this->way2smsHost);
$text = curl_exec($this->curl);
// Check if any error occured
if (curl_errno($this->curl))
return "access error : " . curl_error($this->curl);
// Check for proper login
$pos = stripos(curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL), "ebrdg.action");
if ($pos === "FALSE" || $pos == 0 || $pos == "")
return "invalid login";
// Set the home page from where we can send message
$this->refurl = curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL);
$newurl = str_replace("ebrdg.action?id=", "main.action?section=s&Token=", $this->refurl);
curl_setopt($this->curl, CURLOPT_URL, $newurl);
// Extract the token from the URL
$this->jstoken = substr($newurl, 50, -41);
//Go to the homepage
$text = curl_exec($this->curl);
return true;
}
/**
* #param $phone
* #param $msg
* #return array
*/
function send($phone, $msg)
{
$result = array();
// Check the message
if (trim($msg) == "" || strlen($msg) == 0)
return "invalid message";
// Take only the first 140 characters of the message
$msg = substr($msg, 0, 140);
// Store the numbers from the string to an array
$pharr = explode(",", $phone);
// Send SMS to each number
foreach ($pharr as $p) {
// Check the mobile number
if (strlen($p) != 10 || !is_numeric($p) || strpos($p, ".") != false) {
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => "invalid number");
continue;
}
// Setup to send SMS
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . 'smstoss.action');
curl_setopt($this->curl, CURLOPT_REFERER, curl_getinfo($this->curl, CURLINFO_EFFECTIVE_URL));
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, "ssaction=ss&Token=" . $this->jstoken . "&mobile=" . $p . "&message=" . $msg . "&button=Login");
$contents = curl_exec($this->curl);
//Check Message Status
$pos = strpos($contents, 'Message has been submitted successfully');
$res = ($pos !== false) ? true : false;
$result[] = array('phone' => $p, 'msg' => $msg, 'result' => $res);
}
return $result;
}
/**
* logout of current session.
*/
function logout()
{
curl_setopt($this->curl, CURLOPT_URL, $this->way2smsHost . "LogOut");
curl_setopt($this->curl, CURLOPT_REFERER, $this->refurl);
$text = curl_exec($this->curl);
curl_close($this->curl);
}
}
/**
* Helper Function to send to sms to single/multiple people via way2sms
* #example sendWay2SMS ( '9000012345' , 'password' , '987654321,9876501234' , 'Hello World')
*/
function sendWay2SMS($uid, $pwd, $phone, $msg)
{
$client = new WAY2SMSClient();
$client->login($uid, $pwd);
$result = $client->send($phone, $msg);
$client->logout();
return $result;
}
I am trying to retrieve this page using curl in php. This page of course requires you to log in because it displays different apps for each user. I have been following the work done on this page, however am not having much success.
So far, in his example I am able to successfully populate the auth variable with the auth token. In the next step however (Below the comment for logging into Android Market) I run into troubles. The output variable that he says should have a 302 code results in a "The document has moved" page which links me back to the Google log in page.
Here is a pastebin to show exactly what I am trying. http://pastebin.com/9Fs9GWxk
Additionally if anyone knows what steps I need to do after this to actually get the page I need that would be amazing. Thanks
Here is something I came up with today for this question that has been modified to work for you:
<?php
$USERNAME = 'you#gmail';
$PASSWORD = 'yourpasswd';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:9.0.1) Gecko/20100101 Firefox/9.0.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, COOKIEJAR);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
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&continue=https://market.android.com/mylibrary');
$data = curl_exec($ch);
$formFields = getFormFields($data);
$formFields['Email'] = $USERNAME;
$formFields['Passwd'] = $PASSWORD;
unset($formFields['PersistentCookie']);
// var_dump($formFields);
$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);
//var_dump($result);
if (preg_match('/^2\d{2}/', curl_getinfo($ch, CURLINFO_HTTP_CODE)) == false) {
die("Login failed");
var_dump(curl_getinfo($ch), $result);
} else {
curl_setopt($ch, CURLOPT_URL, 'https://market.android.com/mylibrary');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, true);
$result = curl_exec($ch);
echo $result;
}
function getFormFields($data)
{
if (preg_match('/(<form id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}