curl response for youtube upload - php

I have another question concerning YouTube upload. Based on the answer that I received from khael here (replace form input file with direct file upload), the script uploads the video perfectly. However, the curl response is "Moved Temporarily". What I need instead is to get the YouTube video ID back.
Another problem is that the "nexturl" never gets called. If it was called, I could easily read the video id with "$_GET['id']" and write it to the database.
Here the code I'm working with
$ch = curl_init($response->url."?nexturl=".urlencode($nexturl));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file"=>"#/path/to/file.avi",
"token"=>$response->token
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
Thanks,
Florian

Without testing I'd guess that if you add the FOLLOWLOCATION option you'd get what you're after:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Related

How to POST login info and then GET lander using cURL in PHP?

I am submitting login form data using curl in php, and from what I've observed the login POST has no response - the lander page is loaded by a subsequent GET request. What I'd like to know is how I can make a POST using curl and then a GET? For reference, here's my current code:
$ch = curl_init('https://<my url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html',
));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'my post fields');
$result = curl_exec($ch);
// I need to make a get request to get the loggedin page here!?
curl_setopt($ch, CURLOPT_URL, 'https:<another_url>');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 0);
$result = curl_exec($ch);
echo curl_errno($ch);
I'm currently getting an error 52 from my POST which is no response, which I think should be expected. The issue is, when I tried to make a GET, I also got errno 52. Also for a sanity check, I retrieved my post fields my grabbing the form data from the network data from chrome dev tools.
Thanks in advance for the help!

php curl login on website with different accounts

I've made a php script to login to a website. The script works fine but the problem is that I sometimes have to login on the same website with differents accounts. If I post the second login, the first session still exist and the site redirect me to the login page. If I post the second login again, it works fine.
So my question is how I can kill the previous session with the second login.
EDIT 1: I run the scripts with my browser. After the login it redirects to the website it self. If I close the browser between 2 login posts, it works fine. So the script have to kill the previous session in the browser itself.
Thank you in advance.
my code:
function post_data($url, $data, $header){
$fp = fopen("cookie.txt", "w");
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, realpath("cookie.txt"));
curl_setopt($ch, CURLOPT_COOKIEFILE, realpath("cookie.txt"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_ENCODING, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return curl_exec($ch);
curl_close($ch);
unset($ch);
}
Use a different cookiejar? You could add a 4th argument to add custom curl options to overwrite default options:
http://php.net/manual/en/function.curl-setopt-array.php

Can't receive file_get_contents('php://input'); with data sent with curl

I've been reading some answers, but nothing seems to help in my case, or I'm not able to understand it completelly.
I've got this curl POST, that is working as I can see throught firebug, it sends 1,5MB and I can see the 200 OK
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $my_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
On the $my_url file on another server where I should receive the data I use
$postdata = file_get_contents('php://input');
But Im not receiving anything, I've added
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
as someone suggested in some answers, but still it's not working.
The server who receives the POST has php 5.2.17 I'm not sure if I should activate anything.
Can you helping finding my error, or could you suggest me a different way to send that data?
Thank you in advance
My problem was with curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string); I need to send $data
Note: Not sure if I should delete this question or not.

CURL Post looping

I am running a script which uploads a file to remote form
//submit form
$form_data = array("file_upload" => "#file.xml;type=text/xml","otherkey" => "overvalue");
$ch = curl_init();
$form_url = 'http://www.domain.com/form.php';
curl_setopt($ch, CURLOPT_URL, $form_url);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $form_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
$postResult = curl_exec($ch);
echo $postResult;
The file is being uploaded perfectly, however on the remote server, after file upload and processing the page is reloaded (form.php), however the CURL post simply begins again.
I am guessing this is due to the POST data still being present? how can I do this so that the file is uploaded once, processed and then completed? I have set FOLLOWLOCATION = 0 but with no luck
Many thanks,
How do you know it begins again? If you infer it from your echo, it is echoed twice as you write this:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); //echoes result at init
instead of this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //returns only result, not echoing

stop json stream

I found this code
(full version here http://blog.loicg.net/developpement-web/lire-twitter-stream-php-curl)
function read_the_stream($sTrackingList){
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'https://stream.twitter.com/1/statuses/filter.json');
curl_setopt($ch,CURLOPT_USERPWD,TWITTER_LOGIN.':'.TWITTER_PASSWORD);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, '');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Twitter-Client: ItsMe','X-Twitter-Client-Version: 0.1','X-Twitter-Client-URL: http://rien.net/'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,"track=".$sTrackingList);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'write_callback');
curl_exec($ch);
curl_close($ch);
}
It's work for me but now i don't know how to stop it (for making test it's horrible, my new php file not interpreted!)
You should either put this code inside a script that is not executed on a webpage (like a daemon) or you should not use the stream.
If you want to get only 100 tweets, then use some of the other twitter API for that.
If you want a stop button, consider:
have your other script populate some database (or a queue like redis or ActiveMQ) and then you can have your webpage get your own stream using AJAX
Simply using AJAX to call a script that reads the feeds.

Categories