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.
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 am trying to remote login using the following code:
$cookiefile = dirname(__FILE__)."/cookie.txt" ;
$postData = array(
'&email' => 'email',
'&pwd' => 'password',
// 'redirect_to' => 'http://example.com',
// 'testing_cookie' => '1'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://exaple.com/login?cid=6',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CUROPT_COOKIEJAR => $cookiefile,
CURLOPT_COOKIEFILE=> $cookiefile
));
$output = curl_exec($ch);
echo $output;
I am getting the error - No cookies found -
Sorry, but your browser doesn’t seem to accept cookies from our site. A cookie is a small data package stored in your browser that is needed for personal identification
Update:
Besides I am also getting error like:
Notice: Use of undefined constant CUROPT_COOKIEJAR - assumed 'CUROPT_COOKIEJAR' in E:\wamp\www\remote-curl\curl_request.php
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?
Do any of you know a way to get datas from a database of a website? I already studied and made a program that POST data using json. It gets the shipment status of a certain tracking number and if it's shipped, it will be changed to delivered. And I already done that.
Now what I'm trying to do is get all the datas, or the row of that tracking number and then encode it to JSON. But I don't know what to do.
This is how I did the first program: on getting a specific tracking number and update the status to delivered.
<?php
$url='https://sample.com.ph';
$apikey='apikey';
$method ='SendPackageStatus';
$data = array(
'package' => array(
'tracking_number' => '735898532',
'package_status' => 'delivered',
'failed_reason' => '',
'updated_at' => '2014-01-01 07:02:14'
)
);
$check=json_encode($data);
$postdata = "&data=$check&apikey=$apikey&method=$method";
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $url.'/webservice/',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_VERBOSE => true,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postdata,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded',
)
)
);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
Why not use mysql?... you can store json string on db (as..strings). I dont follow what you are trying to do.
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?