Submit a ASPX form with curl from php - php

The form can be fount here: https://services.smartree.com/mcdonalds/applicationform/WebForms/ApplyToJob.aspx
After I get __VIEWSTATE & __EVENTVALIDATION I start the submit process.
// Start Submit process
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// Populate all POST fields
$postfields = array();
$postfields['__EVENTVALIDATION'] = urlencode($eventValidation);
$postfields['__VIEWSTATEENCRYPTED'] = urlencode('');
$postfields['__VIEWSTATEGENERATOR'] = urlencode($eventGenerator);
$postfields['ucApplyToJob$cmbSelectedJob']=urlencode($values['job_type']);
$postfields['ucApplyToJob$txtLastName'] = $values['lname'];
$postfields['ucApplyToJob$txtFirstName'] = $values['name'];
$p = "";
foreach ($postfields as $k => $v) {
$p .= $k . '=' . $v . '&';
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$ret = curl_exec($ch); // Get result after login page.
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $ret;
print_r($header);
I have populated all required fields but nothing. Please help.

Related

Curl not json response in php

not getting response using curl. I have put all solution but did not get response.
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result); // not display result
this example not displaying any result but it send to specific place.
Add this in your code and then check.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
For your used-case you have to change your code.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.nhs.uk/organisations/FNM60");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response,true);
print_r($result);
here we are getting response without echo on the top of the page but we don't need to that type of out put so we have disabled using span tag.
echo "<span style='display:none;'>"; //to hide the curl response
$ch = curl_init();
$header = array('api_key:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx','Content-Type: application/json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if($postdata!=""){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$responseBody = json_decode($response);
print_r($responseBody);
echo $response; // display 1(one)
curl_close($ch);
echo "</span>";
and result is : 1
check full code

Trying to keep session with curl login in twitter

I'm trying to keep the session along different requests with curl when login into twitter because at some point it logs out from Twitter
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "*******";
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $htmldos, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
var_dump($cookies);
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
But when doing a var_dump in cookies to see if the cookies values are the same, under ["_twitter_sess"] I get different strings in the 2 requests. Shouldn't I get the same string if the session is kept between requests? If so, how do I do that?
This code works for logging in Twitter, and keeping session as for doing other things such after it as sending tweets. I've been using it for 6 months without problem. You need simple_html_dom.php from PHP Simple HTML DOM Parser for it to work
<?php
include_once('simple_html_dom.php');
global $usuario;
$usuario = "universohumor2"; //your twitter account username
$ch = curl_init();
/************************************** COMIENZAN LAS LLAMADAS A FUNCIONES *************************************/
/***************************************************************************************************************/
$vector_retorno = logueo_twitter($ch);
/******************************************************************************************************************/
function logueo_twitter($ch)
{
//$ch = curl_init();
global $usuario;
$sTarget = "https://twitter.com";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie". $usuario ."_tweet.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
if(curl_errno($ch))
{
echo 'error:' . curl_error($c);
}
preg_match('<input name="authenticity_token" type="hidden" value="([a-zA-Z0-9]*)">', $html, $match);
$authenticity_token = $match[1];
if ($authenticity_token == "");
{
preg_match('<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token">', $html, $matchprima);
$authenticity_token = $matchprima[1];
}
$username = $usuario;
$password = "password"; // your twitter account password
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
# display server response
$htmldos = curl_exec($ch);
//echo $htmldos;
if(curl_errno($ch))
{
echo 'error:' . curl_error($ch);
}
$vector["sPost"] = $sPost;
$vector["authenticity_token"] = $authenticity_token;
return $vector;
}
?>

Don't show logs with PHP command and cURL

I developed a script with PHP which I use with the console, with the command php index.php.
In this script, I use cURL to query a server.
The problem is my script shows logs on the console, and I just want the result of the echo.
Do you have an idea to hide these logs?
// GET TOKEN
// /////////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pwd");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$data = curl_exec($ch);
$location = "";
preg_match_all('/^Location:(.*)$/mi', $data, $location);
$location = trim($location[1][0]);
$location = parse_url($location);
parse_str($location['query'], $attrs);
$token = $attrs['code'];
if( isset($token) ) {
// GET CONNEXION
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$post_data = ['code' => $token, 'grant_type' => 'authorization_code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$client_id:$client_secret");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = json_decode(curl_exec($ch));
$connexion = isset($data->access_token) ? $data->access_token : $data->error_description;
// GET LOGIN
// /////////////////////////////////////////////////////////////////////////
$url = "some/url";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
$data = json_decode($data);
// $login = json_encode(['public' => $data->api_keys[1]->public, 'secret' => $data->api_keys->secret]);
// REQUEST
// /////////////////////////////////////////////////////////////////////////
$url = $request . "?externalId=".$external_id."&externalSource=".$external_source;
date_default_timezone_set("Europe/Paris");
$nonce = generateRandomString();
file_put_contents('php://stderr', print_r("Set random nonce to " . $nonce . "\n", TRUE));
$created = date("Y-m-dTH:i:sP");
$created = date("Y-m-dTH:i:sP");
$username = $data->api->public;
$secret = $data->api->secret;
$pwd_digest = base64_encode(sha1($nonce.$created.$secret));
$auth_header = "X-WSSE: UsernameToken Username=\"$username\", PasswordDigest=\"$pwd_digest\", Nonce=\"$nonce\", Created=\"$created\"";
$header = array($auth_header, 'Accept: something', 'Accept-Language: en');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$data = json_decode($data);
echo $data->nbNqPoints;
} else {
echo "0";
}
Answer found at this address: managing curl output in php
I add this line for every cURL instance :
curl_setopt($ch, CURLOPT_VERBOSE, 0);

curl is not collecting all the cookies from external site

for some reason , its not collecting all the cookies, its not collecting the password hash or the member id , im not sure why its not setting those since its getting the others, am i doing somthing wrong with my coding, this is my first time using curl
this is the information in the cookie.txt file
<?php
//init curl
function curl_file_get_contents($url){
$username = 'user#hotmail.com';
$password = 'mypass';
$loginUrl = 'http://forums.zybez.net/index.php?app=curseauth&module=global&section=login';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_REFERER, 'http://forums.zybez.net/runescape-2007-prices/282-law+rune');
$content = curl_exec($ch);
curl_close($ch);
file_put_contents('~/download.zip', $content);
$curl = curl_init();
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_REFERER, $loginUrl);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$contents = curl_exec($curl);
return $contents;
curl_close($curl);
}
function get_input_tags($html){
$post_data = array();
// a new dom object
$dom = new DomDocument;
//load the html into the object
$dom->loadHTML($html);
//discard white space
$dom->preserveWhiteSpace = false;
//all input tags as a list
$input_tags = $dom->getElementsByTagName('input');
//get all rows from the table
for ($i = 0; $i < $input_tags->length; $i++) {
if (is_object($input_tags->item($i))) {
$name = $value = '';
$name_o = $input_tags->item($i)->attributes->getNamedItem('name');
if (is_object($name_o)) {
$name = $name_o->value;
$value_o = $input_tags->item($i)->attributes->getNamedItem('value');
if (is_object($value_o)) {
$value = $input_tags->item($i)->attributes->getNamedItem('value')->value;
}
$post_data[$name] = $value;
}
}
}
return $post_data;
}
/*
Usage
*/
error_reporting(~E_WARNING);
function getauth(){
$html = curl_file_get_contents("http://forums.zybez.net/runescape-2007-prices/282-law+rune");
echo "<pre>";
$auth1 = (get_input_tags($html));
$auth = $auth1["auth"];
print_r($auth1);
}
getauth();
?>

Logging in and downloading content using PHP and CURL

I'm trying to log in to site and then go to one of the pages and retrieve the data. I have a problem with authorization.
Sample code:
$loginUrl = 'https://strona.pl/authorization'; //action from the login form
$loginFields = array('username'=>'mail#mojmail.pl', 'password'=>'haslo'); //login form field names and values
$remotePageUrl = 'https://strona.pl/pdstrona'; //url of the page you want to save
$login = getUrl($loginUrl, 'post', $loginFields); //login to the site
$remotePage = getUrl($remotePageUrl); //get the remote page
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
$buffer = curl_exec($ch);
//jeżeli pojawił się błąd to go wyświetlimy
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $buffer;
print $status;
}
// wyłącza pokazywanie błędów dla funkcji loadHTML
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom ->strictErrorChecking = FALSE;
$dom->loadHTML($remotePage);
$xpath = new DOMXpath($dom);
As a result of this query I am redirected to the main page (not the sub). What is important, I enter email address into a script in encoded form. Example: email% 40mojmail.pl
edit
Ok - I will add some information . I am a publisher and working with afilo.pl . Daily checking of rates is very tiring, so I wanted to prepare a script that will collect data once a day and informed me of the changes.
Unfortunately I can not retrieve the data.
Sample cookies from my browser :
Set- Cookie: PHPSESSID = jat102p33s0pmfairri1qiih24 ; expires = Thu, 25 -Dec- 2013 9:16:40 p.m. GMT ; path = / ; domain = . Afilo.pl
I modified the code but it still does not work.
loginUrl = 'https://opentrack.afilo.pl/logowanie'; //action from the login form
$loginFields = array('loginemail'=>'.......','loginhaslo'=>'........');
//login form field names and values
$remotePageUrl = 'https://opentrack.afilo.pl/partner/programy-lista'; //url of the page you want to save
$login = getUrl($loginUrl, 'post', $loginFields); //login to the site
$remotePage = getUrl($remotePageUrl); //get the remote page
function getUrl($url, $method='', $vars='') {
$ch = curl_init();
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
session_start();
$strCookie = session_name() . '=' . $_COOKIE[ session_name() ] . '; path=/; domain=.afilo.pl';
session_write_close();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookies.txt');
//curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookies.txt');
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
$buffer = curl_exec($ch);
//je¿eli pojawi³ siê b³¹d to go wyœwietlimy
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $buffer;
print $status;
}
This is the answer to my question. This code works well.
// options
$EMAIL = 'login';
$PASSWORD = 'haslo';
$cookie_file_path = "/cookies/cookies.txt";
$LOGINURL = "https://domainname.com/auth";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
$fields = array();
$fields['loginemail'] = $EMAIL;
$fields['loginhaslo'] = $PASSWORD;
$POSTFIELDS = http_build_query($fields);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
$result = curl_exec($ch);
$remotePageUrl = 'https://domainname.com/partner/programy-lista';
curl_setopt($ch, CURLOPT_URL, $remotePageUrl);
$result = curl_exec($ch);

Categories