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?
Related
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.
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
I have php a code and can't understand why the php script creates only 4 files, but without the data. If I use curl only for one url without foreach loop all will be ok. Any thoughts?
$years = array('2012', '2013', '2014', '2015');
foreach($years as $year)
{
$url = "http://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_{$year}.txt";
$fp = fopen(base_path() . "/database/rawdata/{$year}.txt", 'w');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FAILONERROR => 1
));
curl_exec($curl);
if (!curl_errno($curl))
{
$info = curl_getinfo($curl);
Log::info("File created {$year}.txt");
Log::notice("Evaluation of this script was {$info['total_time']} seconds!");
}
else
{
Log::error("cURL ERROR " . curl_error($curl));
}
curl_close($curl);
fclose($fp);
}
See joeterranova's comment on the PHP site:
It appears that setting CURLOPT_FILE before setting
CURLOPT_RETURNTRANSFER doesn't work, presumably because
CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set
.
Therefore you should change your curl_setopt_array to the following:
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_FAILONERROR => 1
));
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.
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?