PHP - Mysterious Error in Upload script - php

i'm trying to upload a file to vshare.io trough their api. They're offering a php script to do so:
<?php
if(!function_exists('curl_init')) {
die('CURL functions are not available. Debian: apt-get install php5-curl');
}
$file_path = ''; // Example: $file_path = '/home/files/file.exe';
$token = ''; // You can get your TOKEN from the following page http://vshare.io/api.html
$post = array(
'token' => $token,
'filesize' => filesize($file_path),
'Filedata' => '#' . $file_path
);
// init
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://upload.vshare.io/upload_api.php');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$output = json_decode($result, TRUE);
if(isset($output['upload'], $output['video'], $output['fileid']) && strlen($output['fileid']) == 7 && $output['upload'] == 1) {
if($output['video'] == '1') {
$file_type = 'video';
} elseif($output['video'] == '0') {
$file_type = 'file';
}
echo 'File Type: ' . $file_type . ' | File Link: http://vshare.io/d/' . $output['fileid'];
} else {
echo 'Error: ' . $output['msg'];
}
?>
i inserted my token and the correct filepath in Ubuntu, however when launching the script it will run for about 1 minute and then it will print "Error:" (last line of the script say to do so). No files are uploaded to my account
Any hint?

There could be 2 reasons. First is your user-agent, you have not set it. The most site today restricted BOT so you should set it for your CURL:
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
The second reason is that the CURL process time out so you got nothing nor any message.
Try this
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 400);

Related

OTRS CSS/js not loading when using with mod_auth_cas and login through API and CAS ticket

Team,
I have created SSO using CAS, php application and OTRS
I am getting logged in into php application through CAS server using CAS apis.
From index.php php application code:
$ckfile = tempnam("/tmp", "CURLCOOKIE");
$useragent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36';
/**
* Get __VIEWSTATE & __EVENTVALIDATION
*/
$ch = curl_init(CAS_URL);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$html = curl_exec($ch);
$errorNo = curl_errno($ch);
$curlError = curl_error($ch);
curl_close($ch);
if ($errorNo == false) {
} else {
return $return = array(
'type' => 'error',
'message' => 'Curl error: ' . $curlError . ' ' . $errorNo,
'serviceURL' => '',
);
}
$serviceValue = '';
$dom = new DOMDocument();
# Parse the HTML
# The # before the method call suppresses any warnings that
# loadHTML might throw because of invalid HTML in the page.
#$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('form') as $input) {
# Show the attribute value
$serviceValue = $input->getAttribute('action');
}
now using this service value to get ST ticket
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serviceValue);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "service=https://10.10.1.80/otrs");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$ticket = curl_exec($ch);
$errorNo = curl_errno($ch);
$curlError = curl_error($ch);
curl_close($ch);
if ($errorNo == false) {
} else {
return $return = array(
'type' => 'error',
'message' => 'Curl error: ' . $curlError . ' ' . $errorNo,
'ticket' => '',
);
}
Now I am using this ST ticket to hit otrs as
https://10.10.1.80/otrs/customer.pl?ticket=$ticket
I am able to successfully login into the OTRS but the UI gets distorted as otrs is unable to render following css and js file with URLs:
https://10.10.1.80/otrs-web/skins/Customer/default/css-cache/CommonCSS_102b06602eb972432ddd82f2c05c1b80.css
It is hitting CAS login call back url set on the httpd.conf file
But I logged into through the API not the application itself
I don't know how to resolve the issue
Please provide some work around
Thanks

Bypass age check on Steam website (with PHP curl)

I know, there is already a thread about this ... see How to pass the steam age check using curl? ... but I'm a new user and can't comment in an existing thread and the answer marked as solution there doesn't work anymore.
I had my own code that worked fine in the past (around 2017), but doesn't work anymore as well.
Here is my code that worked in the past:
function curl_redirect_exec2($ch, &$redirects, $curlopt_header = false) {
$ckfile = tempnam(sys_get_temp_dir(), "CURLCOOKIE");
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'snr=1_agecheck _agecheck__age-gate&ageDay=1&ageMonth=May&ageYear=1990');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
//new start
curl_setopt($ch, CURLOPT_COOKIE, 'mature_content=1; path=/app/'.$gameid.';');
//new end
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($http_code == 301 || $http_code == 302) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match('/(Location:|URI:)(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
$url_parsed = parse_url($url);
if(isset($url_parsed)) {
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return curl_redirect_exec2($ch, $redirects);
}
}
if($curlopt_header) {
return $data;
} else {
list(,$body) = explode("\r\n\r\n", $data, 2);
return $body;
}
}
And here is the code sample from the thread linked above that also seemed to work in the past (but doesn't anymore):
<?php
$url = "http://store.steampowered.com/app/312660/";
// $file = __DIR__ . DIRECTORY_SEPARATOR . "cookie.txt";
// $postData = array(
// 'ageDay' => '31',
// 'ageMonth' => 'July',
// 'ageYear' => '1993'
// );
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13");
curl_setopt($ch,CURLOPT_POSTFIELDS,$postData);
// curl_setopt($ch,CURLOPT_COOKIESESSION, true);
// curl_setopt($ch,CURLOPT_COOKIEJAR,$file);
// curl_setopt($ch,CURLOPT_COOKIEFILE,$file);
$strCookie = 'mature_content=' . 1 . '; path=/';
curl_setopt( $ch, CURLOPT_COOKIE, $strCookie );
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
What I tested so far:
You can use the game "RUST" as an example: https://store.steampowered.com/app/252490/
Page redirects to age check: https://store.steampowered.com/agecheck/app/252490/
I saw that the cookie set uses other names now ("wants_mature_content" instead of "mature_content" in the JavaScript), but even after changing the PHP to use the new name, it doesn't work.
JavaScript code from Steam page:
function HideAgeGate( )
{
var bHideAll = false;
console.log(bHideAll);
var strCookiePath = bHideAll ? '/' : "\/app\/252490";
V_SetCookie( 'wants_mature_content', 1, 365, strCookiePath );
document.location = "https:\/\/store.steampowered.com\/app\/252490\/Rust\/?snr=";
}
Edit: I also found the function "V_SetCookie" ... in https://store.akamai.steamstatic.com/public/shared/javascript/shared_global.js ... that is called by the code above:
function V_SetCookie( strCookieName, strValue, expiryInDays, path )
{
if ( !path )
path = '/';
var strDate = '';
if( typeof expiryInDays != 'undefined' && expiryInDays )
{
var dateExpires = new Date();
dateExpires.setTime( dateExpires.getTime() + 1000 * 60 * 60 * 24 * expiryInDays );
strDate = '; expires=' + dateExpires.toGMTString();
}
document.cookie = strCookieName + '=' + strValue + strDate + ';path=' + path;
}
Can somebody help please? :-) Thanks!
This is working for me
<?php
$url = 'https://store.steampowered.com/bundle/5699/Grand_Theft_Auto_V_Premium_Edition/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Cookie: birthtime=470682001;lastagecheckage=1-0-1985;']);
$html = curl_exec($ch);
curl_close($ch);
var_dump($html);
Ok, got it working.
There are actually 3 cookies: "wants_mature_content", "lastagecheckage" and "birthtime"
Open a page with age check in e.g. Chrome, click on "View Page" and then look for the 3 cookies in chrome (and their content). Set all 3 cookies with PHP's curl and it's working ;-)

file_get_contents not retrieving page contents

OK, before saying this is a duplicate just read a bit....
I have been trying to echo contents of URL that has allow_url_fopen disabled for HOURS now, I have tried every solution posted on stack overflow. EXAMPLE:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
$result = curl_exec($ch);
curl_close($ch);
Doesn't WORK
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Doesn't WORK
$url = "http://www.google.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
Doesn't WORK
fopen("cookies.txt", "w");
$url="http://adfoc.us/1575051";
$ch = curl_init();
$header=array('GET /1575051 HTTP/1.1',
'Host: adfoc.us',
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:en-US,en;q=0.8',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Host:adfoc.us',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
$result=curl_exec($ch);
curl_close($ch);
Doesn't WORK
// create the Gateway object
$gateway = new Gateway();
// set our url
$gateway->init($url);
// get the raw response, ignore errors
$response = $gateway->exec();
Doesn't WORK
$file = "http://www.example.com/my_page.php";
if (function_exists('curl_version'))
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $file);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($curl);
curl_close($curl);
}
else if (file_get_contents(__FILE__) && ini_get('allow_url_fopen'))
{
$content = file_get_contents($file);
}
else
{
echo 'You have neither cUrl installed nor allow_url_fopen activated. Please setup one of those!';
}
This doesn't work.
The page I am trying to use file_get_contents on is not on my website. I am trying to use file_get_contents so i can make a simple API for the site owner by reading a page and checking if a certain word is present on the page.
But yeah if anyone has any suggestions PLEASE post below :)
You can check first weather the site is available or not for example a sample code
Code taken from here:
<?php
$cURL = curl_init('http://www.technofusions.com/');
curl_setopt ( $cURL , CURLOPT_RETURNTRANSFER , true );
// Follow any kind of redirection that are in the URL
curl_setopt ( $cURL , CURLOPT_FOLLOWLOCATION , true );
$result = curl_exec ( $cURL );
// Getting HTTP response code
$answer = curl_getinfo ( $cURL , CURLINFO_HTTP_CODE );
curl_close ( $cURL );
if ( $answer == ' 404 ' ) {
echo ' The site not found (ERROR 404)! ' ;
} else {
echo ' It looks like everything is working fine ... ' ;
}
?>
For a full answer you can got to this tutorial Curl IN PHP

Curl Option for page without html

I am creating a nagios that will automatically login to my website and display result in Nagios. Currenlty I am able to login using script but my PHP Curl script return full HTML code after login thourgh curl.
here is my script.
<?php
$id = "username";
$pw = "password";
$postfields = "userName=farooq&password=hussaind";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1); // Get the header
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // Allow redirection
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie");
curl_setopt($ch, CURLOPT_URL,"http://abc.com/signin.htm");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) ");
curl_setopt($ch, CURLOPT_POSTFIELDS, "$postfields");
curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
# echo $statusCode;
if ($statusCode >= 200) {
echo 'OK - Status Code = '. $statusCode . '. Successfully Login' ;
exit (0);
}
else if ($statusCode >= 400){
echo 'CRITICAL - Status Code = '. $statusCode . '. Unable to loging. Please check ';
exit(2);
}
else {
echo 'UNKOWN - Status Code = '. $statusCode . '. Unable to loging. Please check ';
exit(1);
}
curl_close($ch);
?>
I just want simple message to print without any HTML crap. Like
OK - Status Code = 200 Successfully Login
Please help me in this regard.
Thanks
Farooq Hussain
Set CURLOPT_RETURNTRANSFER to true.
http://www.php.net/manual/en/function.curl-setopt.php

cURL - File is not downloaded when using curl

For what i'm trying to do, i use PHP5 in CLI, and cURL extension.
I'm trying to download a file from youtube's server, it works fine with any navigator,
the link is something like that;
`http://youtube.com/get_video_info?video_id=VIDEO_ID
exemple: http://youtube.com/get_video_info?video_id=9pQxmD6Bhd
When i access this file trough my navigator, it prompt me with a download box for the file
'get_video_info', when downloaded the file content some data, ..
The problem is to get this file with cURL, i keep getting this error message;
status=fail&errorcode=2&reason=Invalid+parameters.
This is the code ( i tried to change some option, but i'm not familliar with cURL, so i'm stuck.
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=9pQxmD6Bhd");
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false)
{
trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
}
else
{
var_dump($output);
}
curl_close($c);
I tried to use some curl_setopt options, like CURLOPT_TRANSFERTEXT with no success.
I definitely need help !
Thanks for answers, and sorry if i did something that dont respect the rules here, it's my first post.
EDIT
Here is the code to download youtube video ( .ogg ) with php in cli.
<?php
/*Youtube URL and ID*/
$youtube_video = "http://www.youtube.com/watch?v=Ftud51NhY2I";
$yt_id = explode("=", $youtube_video);
$id = $yt_id[1];
/*
Functions
*/
function get_link($raw){
$url = rawurldecode(rawurldecode($raw));
$url = explode("&qual", $url);
return $url[0];
}
/*
Here we go
Query video token
*/
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $youtube_video);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false)
{
trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);
}
else{}
curl_close($c);
/*
Get Video infos
*/
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=".$id);
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($c, CURLOPT_HEADER, false);
$output = curl_exec($c);
if($output === false){trigger_error('Erreur curl : '.curl_error($c),E_USER_WARNING);}
else{}
curl_close($c);
/*Get RAW link*/
$temp = explode("url_encoded_fmt_stream_map=url%3D", $output);
$url = explode("=", $temp[1]);
$url = get_link($url[0]);
/*Get Video name*/
$temp = "";
$temp = explode("title=", $output);
$title = explode("&", $temp[1]);
$title = rawurldecode(rawurldecode($title[0]));
$replace = array(':', '+', '\\', '/', '"', '<', '>', '|', '(', ')', '\'');
$title = str_replace($replace, ' ',$title);
//echo $title;
/*
Download Video
*/
$url = $url;
$path = $title.'.ogg';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
echo "Done... \r\n";
?>
You get error message because the video_id parameter isn't valid.
Try changing that ID and it should work correctly.
http://www.youtube.com/watch?v=9pQxmD6Bhd - does not exist
youtube has changed their system. now it is working only with the real IP who use the get_video_info system. when you try with cURL it sends the server IP to Youtube, then you have to download videos with the servers IP, because youtube system creates the direct video download urls with given IP.

Categories