First of all I'm aware of curl_multi_init but it doesn't exactly do what I need, to my knowledge.
Im downloading files behind a login, so
a) Need to login
b) Download image(s)
Here is what I currently have
$login_url = 'https://us.test.com/Member/Login';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$login_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'login='.$this->username.'&password='.$this->userpass.'&signin=1&login_referer=none&remember_me=1');
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/test/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, true);
curl_exec($ch);
//Grab and Write the image
$image = file_get_contents($image_url);
$f = fopen($_SERVER['DOCUMENT_ROOT'].'/test/uploads/'.$id.'.jpg', 'w');
fwrite($f, $image);
fclose($f);
curl_close($ch);
Anything helps, much appreciated.
I figured it out, just added session ID into cURL. But a quick and dirty way is
$download_str = 'wget -O '. $save . $id . '.jpg --cookies=off --header "Cookie: PHPSESSID=' . $session_id . '" ' . $image_url;
shell_exec($download_str);
Related
Is there any way to get file contents of external webiste, say https://www.example.com/.
For eg. Using Python from shell_exec. I tried python but got blank.
Not sure why you want to use shell_exec but
you can use
$data = file_get_contents('https://www.example.com/');
or
$url = 'https://www.example.com/';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
if you want to maintain the session you could use curl with CURLOPT_COOKIEFILE
eg:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiefile");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com/');
$data = curl_exec($ch);
or if you really want to use the shell_exec the use follwing
$data = shell_exec('curl -I https://www.example.com/');
echo "<pre>$data</pre>";
however for above to work you have to have curl installed check following for instillation of curl
https://curl.se/
hope this works
How can I Create, update and delete github code (file) programatically using php (preferably curl) and github api without using any external library ?
I cannot find anything about this. Is this possible or not ?
Probably the safest/most proper solution would be to use the Github API. You can find the documentation on creating a commit here: https://developer.github.com/v3/git/commits/#create-a-commit
I know you're not wanting to use a library, but this library would probably make this a lot easier: https://packagist.org/packages/knplabs/github-api
If you're not using a library, you'll need to figure out how to make sure your request is properly authenticated: https://developer.github.com/v3/#authentication
Finally I've resolved it.
<?php
function pushFile($username,$token,$repo,$branch,$path,$b64data){
$message = "Automated update";
$ch = curl_init("https://api.github.com/repos/$repo/branches/$branch");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent:Php/Automated'));
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
$data=json_decode($data,1);
$ch2 = curl_init($data['commit']['commit']['tree']['url']);
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('User-Agent:Php/Ayan Dhara'));
curl_setopt($ch2, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch2, CURLOPT_TIMEOUT, 30);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, TRUE);
$data2 = curl_exec($ch2);
curl_close($ch2);
$data2=json_decode($data2,1);
$sha='';
foreach($data2["tree"] as $file)
if($file["path"]==$path)
$sha=$file["sha"];
$inputdata =[];
$inputdata["path"]=$path;
$inputdata["branch"]=$branch;
$inputdata["message"]=$message;
$inputdata["content"]=$b64data;
$inputdata["sha"]=$sha;
echo json_encode($inputdata);
$updateUrl="https://api.github.com/repos/$repo/contents/$path";
echo $updateUrl;
$ch3 = curl_init($updateUrl);
curl_setopt($ch3, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'User-Agent:Php/Ayan Dhara'));
curl_setopt($ch3, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch3, CURLOPT_TIMEOUT, 30);
curl_setopt($ch3, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch3, CURLOPT_POSTFIELDS, json_encode($inputdata));
$data3 = curl_exec($ch3);
curl_close($ch3);
echo $data3;
}
//pushFile("your_username","your_personal_token","username/repository","repository_branch","path_of_targetfile_in_repository","base64_encoded_data");
?>
The task - there is a direct link to download the file, you need to use php methods to download this file and save it on the server.
There is code, but it does not load the file, but only creates a new one.
<?php
$token = 'token';
// file from yandex disk.
$yd_file = '/FermerPrice/test.jpeg/';
// save this.
$path = download;
//get array with link
$ch = curl_init('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$res = curl_exec($ch);
curl_close($ch);
// $res['href'] - link download
$res = json_decode($res, true);
if (empty($res['error'])) {
$file_name = $path . '/' . basename($yd_file);
$file = #fopen($file_name, 'w');
$ch = curl_init($res['href']);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: OAuth ' . $token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($file);
echo "Good.";
}
?>
You can use this:
file_put_contents("localfilename", file_get_contents('https://cloud-api.yandex.net/v1/disk/resources/download?path=' . urlencode($yd_file)));
Hi I'm trying to get data from anther site so I used
$url = 'MY_URL' . $route . '/' . $id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('url: ACCESS_URL', 'token: TOKEN'));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, $type);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$output = curl_exec($ch);
curl_close($ch);
return $output;
so I get 301 Moved Permanently output
I works fine on local machine put after uploading I got that error
,any suggestions
the problem with me was missing component curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
You need to specifically tell cURL to follow redirection:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
So, I've been trying to execute the function Cron::add_line using an script that I have builded and it didn't work properly yet. The script login into cPanel but in the second part it don't execute the json-api to execute the function add_line, I've did many searches about this but never found anything to make me comprehend how it works in cURL.
If I type the command url in my browser, it works and create the cron, otherwise, using my script it doesn't work.
If anyone could help I appreciate!
Thanks!
Code:
<?php
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$site = 'https://' . $_REQUEST['site'] . ':2083';
$auth_url = $site . '/login/?';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $auth_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'user=' . $username . '&pass=' . $password);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
$h = curl_getinfo($ch);
if ($result == true && strpos($h['url'], "cpsess"))
{
$pattern = "/.*?(\/cpsess.*?)\/.*?/is";
$preg_res = preg_match($pattern, $h['url'], $cpsess);
}
$cpcmd_url = $site . "$ENV{'" . $cpsess[1] . "'}" . '/json-api/';
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $cpcmd_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'cpanel?user=$username&cpanel_jsonapi_module=Cron&cpanel_jsonapi_func=add_line&cpanel_jsonapi_version=2&command=GET http://www.cdihost.com.br/admin/cron.php&day=*&hour=*&minute=*/5&month=*&weekday=*');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100020);
$result = curl_exec($ch);
curl_close($ch);
?>
Try some debugging, like error_log($cpcmd_url); to see if the url is correct. Also, in CURLOPT_POSTFIELDS, cpanel? seems wrong to me.