i'm stuck to try handle a random name and value of form html to post data through curl.
here my code.
function grapvalue($html){
//parse content to grap name and value random
}
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => grapvalue($html)
)
$init = curl_init("site.com")
curl_setopt_array($init,$opt)
$html = curl_exec($init)
I have to execute the function curl_exec () twice, first I have to execute the curl_exec function to retrieve the html to get the names and random values, then send the data.
how do I resolve this?
Here is the full solution:
//First, we make a request to grab the variables
function grapvalue(){
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
);
$init = curl_init("site.com");
curl_setopt_array($init,$opt);
$html = curl_exec($init);
//parse content to grap name and value random
}
//Than, just call grapvalue() without any parameters
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => grapvalue()
);
$init = curl_init("site.com");
curl_setopt_array($init,$opt);
$html = curl_exec($init);
Tip: you may prefer using CURLOPT_COOKIE rather than CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR.
Why you don't create a function cURL(), call the function, get the $html and call again with the post values?
Related
Hi I'm trying to setup a PHP CURL call using OAuth1 authorization method.
I've tried with POSTMAN 1st, to generate the PHP code. I've completed it with the necessary datas
<?php
$conskey = 'XXXXXXX';
$conssec = 'XXXXXXX';
$nonce = mt_rand();
$timestamp = time();
$url = 'https://some-website/api/project/subproject';
$method = 'POST';
$oauth = new OAuth($conskey, $conssec, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->setNonce($nonce);
$oauth->setTimestamp($timestamp);
$signatureOAuth = $oauth->generateSignature($method, $url);
$curl = curl_init($url);
curl_setopt_array($curl, array(
CURLOPT_URL => $url.'?oauth_consumer_key='.$conskey.
'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='.$timestamp.
'&oauth_nonce='.$nonce.
'&oauth_version=1.0&oauth_signature='.$signatureOAuth,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $jsonDatas,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
// THE COOKIE I WANNA GET
'Cookie: SSESSd4f3e89d4699e1d1a071aa37eab4fcEd=DWS4UqpaykI2y7q-HJXEzGN82AKHQYnWo5hbsqkAqiQ'
),
));
$result = curl_exec($curl);
curl_close($curl);
But I've noticed that there's the cookie in the CURLOPT_HTTPHEADER entry but I don't have any idea how POSTMAN generate this cookie.
Without this cookie or with a dumb string, the CURL response is always Invalid Signature
Postman is not generating cookies for you, and neither is curl/php. you either did some prior requests (usually GET to get cookies) requests to the website where you received some cookies, or gave the cookies to postman some other way.
since you're not showing us the real url, we can only speculate, but to take an example, here is how to get a session cookie for stackoverflow.com:
<?php
$ch=curl_init();
curl_setopt_array($ch,array(
CURLOPT_COOKIEFILE => "", // setting it to empty activates the curl cookie engine (its disabled by default.),
CURLOPT_URL => "https://stackoverflow.com/",
CURLOPT_RETURNTRANSFER => true,
));
$html=curl_exec($ch);
$cookies = (function($cookielist):array{
$cookies = array();
foreach($cookielist as $cookie_raw) {
$chunks = explode("\t", $cookie_raw);
//var_dump($chunks);
$cookie['domain'] = $chunks[0];
$cookie['secure'] = $chunks[1];
$cookie['path'] = $chunks[2];
$cookie['???todo_wtf_is_this'] = $chunks[3];
$cookie['expiration'] = $chunks[4];
$cookie['name'] = $chunks[5];
$cookie['value'] = $chunks[6];
$cookies[] = $cookie;
}
return $cookies;
})(curl_getinfo($ch, CURLINFO_COOKIELIST));
var_export($cookies);
prints something like
array (
0 =>
array (
'domain' => '#HttpOnly_.stackoverflow.com',
'secure' => 'TRUE',
'path' => '/',
'???todo_wtf_is_this' => 'FALSE',
'expiration' => '2682374400',
'name' => 'prov',
'value' => '9c06f038-9f70-bee8-2a64-b095656175d1',
),
)
which could be used like
// THE COOKIE I WANNA GENERATE
'Cookie: '. $cookies[0]["name"].'='.$cookies[0]["value"]
but this is very rarely done, usually you'd just use curl's built-in cookie engine to handle cookies automatically..
I want to download a repository file from github after that the user doing an action. The code will not work as expected. A .zip file is created when the code run but the resulting zip file is blank. How I can fix this?
$url = "https://github.com/$u/$repo/archive/master.zip";
$ch = curl_init();
$f = fopen(__DIR__.'/master.zip', 'w+');
$opt = [
CURLOPT_URL => $url,
CURLOPT_FILE => $f,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
];
curl_setopt_array($ch, $opt);
$file = curl_exec($ch);
curl_close($ch);
fclose($f);
Remove CURLOPT_RETURNTRANSFER => true from your code or put it before CURLOPT_FILE.
This is known interaction as CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.
Currently I have the following code.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('simple_html_dom.php');
$df = upload('username','password','song.mp3','song.mp3');
$df = json_decode($df, true);
function upload($login,$password,$filename,$file) {
$ch = curl_init();
$postData = array(
'op' => 'login',
'login' => $login,
'password' => $password,
'returnto' => '/'
);
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/login.html',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://dopefile.pk/?op=upload");
$output = curl_exec($ch);
$html = str_get_html($output);
$ret1 = $html->find('form[id=uploadfile]');
$ret = $html->find('form[id=uploadfile] input');
foreach($ret AS $r) {
$newPost[$r->name]=$r->value;
}
$newPost['file_0'] = '#'.$file.';filename='.$filename;
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/cgi-bin/upload.cgi?upload_type=file',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $newPost,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
return $output = curl_exec($ch);
}
Which is trying to upload to the file sharing website the file song.mp3 which is within my directory, and then return the file sharing websites link (if you download the simple_html_dom.php files and try and run the code above, you can see that it returns no errors or returns no file sharing link + does not upload to my account on the website...
The cookies file updates with the cookies that show I successfully logged in, how ever I can't get the actual file link returned can anyone help me please?
I am trying to submit the form at https://www.igolder.com/PGP/encryption/ and return the results from the "Encrypted Message" textarea using PHP and cURL. I can see the page with the following code but I don't know if it is submitting the form and I don't know how to get the results. Also, yes, =NXwT is meant to be on a separate line in PGP Public Key textarea. Any feedback would be much appreciated! Here is my code below:
//create array of data to be posted
$post_data['ctl00$cp$txtPublicKey'] = 'mQENBFfWrk0BCACPDJk8f6GOpQewp0AtP5CrhstpazsYPG9r+8wWamSgVYbIwdS8/bdnVdIu7Be680m3VcjtZWwG9wiomTXCx5RargREqNICigzPTU0f/CgHEMFoZQn2bWz1YXBrpYb+qgxMweVe1T4uz8sOeHzAebMAQCdNVEwDu4o90vi+Pz3jqgSEpJyhe4RqOgEdkkrtGaEUnNr2IO8gCSmPAeQW6qFNcJsl7iWfeY7LHMv6A77n/1mGhdp4h0E+V6ElUNPjeEj9I1QYSIXRxDcejvcq5HrF27DyQ1mN1TgdauAkGdqRuIJsMDmEaYtLm3fIAwpbiAcJu6fmz7eAnBz5scTlLvEZABEBAAG0GmR1c3Rpbm9ybW9uZEBjcmVpZ2h0b24uZWR1iQEcBBABAgAGBQJX1q5NAAoJEKd43LZ3nczviUoIAIAzstgpn++GAfwHs7wMdjQbKvK3LB7hZKfskiATVWxafRvNGP9Gv0PPF+ly5WLt0PjLsOc6KV98T5jtF3h0rvTZK4viZJD6JFAgiaWzL0+w5a/ksUMEgMdx4xHM/k+sSbtBlha+GI72IMsU/0dKrYkSkZ5BpUoepk5OtRvu37Pg5dSW0Zx/cd+8bnVMBK6Ri8yT0Lvg+5w0KSCn5LvXtpGKc87TJgMBHWRa3qikmw3XWqPCza+APbTjZ/WlLtA0lpP7txfbYsTQObVdiZ7DFOb1n4OxpWxrvV8e60VazKBmv4daFyim+qWySsvYCBmwn8+8r+J/5OdKhG7tAW71qxY=
=NXwT';
$post_data['ctl00$cp$txtMsgToEncrypt'] = 'Test Message';
$post_data['ctl00$cp$btnGeneratePgpKeys'] = 'Encrypt Message';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . urlencode($value);
}
//create the final string to be posted using implode()
$post_string = implode('&', $post_items);
//create cURL connection
$ch = curl_init();
// sets the cURL options
$options = array(
CURLOPT_URL => "https://www.igolder.com/PGP/encryption/",
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => $_SERVER["HTTP_USER_AGENT"], // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 30, // time-out on connect
CURLOPT_TIMEOUT => 30, // time-out on response
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_string
);
curl_setopt_array($ch, $options);
// execute cURL request
$result = curl_exec($ch);
// prints the cURL result
print_r($result);
/******* instead of printing the entire page, I want to just get the value in the textarea with the name of ctl00$cp$txtEncryptedMessage *******/
//close the connection
curl_close($ch);
?>
I know there are loads of question like this, but could anyone help me narrow down what the issue is?
This is my code:
ini_set("display_errors", 1);
error_reporting(E_ALL);
$cookies = 'cookies.txt';
$postdatal = array(
'__VIEWSTATE' => '', // need a __VIEWSTATE, even if it's empty
'username' => 'username',
'password' => 'password',
'button1' => 'Sign+in', // need button1
'__VIEWSTATEGENERATOR' => '' // same reason as __VIEWSTATE
);
$ch = curl_init();
curl_setopt_array(
$ch, array(
CURLOPT_URL => 'url',
CURLOPT_RETURNTRANSFER => true, // return the results
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postdatal, // send the data
CURLOPT_HEADER => true,
CURLOPT_COOKIEJAR => $cookies, // set the cookies
CURLOPT_COOKIEFILE => $cookies
));
$output = curl_exec($ch);
echo $output;
The headers returned have three Set-Cookie headers. Is there any way I could debug this? I don't seem to get any errors, even if I choose an invalid file name (cookies.txt is a chmod 777 empty text file).
I solved my problem: I needed to add curl_close() in order for the cookie file to be saved.