Jump to content

[SOLVED] cURL (post)


desithugg

Recommended Posts

Well I'm trying to get php to save images onto Imageshack using cURL extensions.

First I tried using an html form and submit, it works perfect. Now to replicate the effect in php i used cURL.

<form method="post" action="http://www.imageshack.us/transload.php" enctype="multipart/form-data" id="upform">
<input type="text" id="fileupload" value="" name="url" size="50" value="http://pn-network.net/sprites/tc_012_2.png">
<input type="hidden" name="MAX_FILE_SIZE" value="13145728">
<input type="hidden" name="refer" value="">
<input type="hidden" name="brand" value="">
<input id="butan" style="width:135px" type="submit" value="host it!" >
</form>

Here's the cURL code.

<?php
$post_data = array(
"url" => "http://pn-network.net/sprites/tc_012_2.png",
"MAX_FILE_SIZE" => "13145728",
"refer" => "",
"brand" => "",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/transload.php");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
?>

But for some reason the page comes up blank, I've got no clue why. It works with everything else.

 

Link to comment
Share on other sites

You may need to use:

 

CURLOPT_FOLLOWLOCATION

CURLOPT_MAXREDIRS

or

CURLOPT_AUTOREFERER

 

if there are any redirections going on during the post to Imageshack. The rest of your code seems fine, but just to make sure, you might want to send the curl post to a test page and print the post variables. Just for your viewing pleasure, I'm including a curl post that I made up so you can see how I did it on my project.

 

<?php
if ($aCleanedValues['opt-in'] == 'Y'){
$prename = $_POST['realname'];
$nameRegex = "/^[A-Z\s\.,'-]+$/i";
$preemail = $_POST['email'];
$emailRegex = "/^(?:^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|gov|mobi|tv|biz|info|jobs|museum)$)$/i";
if(preg_match($nameRegex,$prename) && preg_match($emailRegex,$preemail)) {
	$name = $prename;
	$email = $preemail;
	$url = "http://newsletters.somedomain.com/subscribe.aspx?Task=Join";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$url); // set url to post to
	curl_setopt($ch, CURLOPT_FAILONERROR, 1); //needed for some reason
	curl_setopt($ch, CURLOPT_TIMEOUT, 30); // times out after 31s
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //so you dont see the process, it just happens before the redirect
	curl_setopt($ch, CURLOPT_POST, 1); // set POST method
	curl_setopt($ch, CURLOPT_POSTFIELDS,  array('Name'=>"$name",
												'Email'=>"$email",
												'DeliveryFormat'=>'HTML',
												'Password'=>'RandomNumber',
												'NewsletterListID'=>'526',
												'JoinType'=>'Menu',
												'SID'=>'{CDFEB2A7-0B0C-4541-A50B-CC975DDB114A}')); // add POST fields
	curl_exec($ch); // run the whole process
	curl_close($ch);
}
}
?>

 

One thing that I'm wondering about is how imageshack is going to know you are logged in?

Link to comment
Share on other sites

Umm Yea imageshack does redirect when a form is submit. I tried adding the autorefer and followlocation but it still didn't work.

 

<?php
$post_data = array(
"url" => "http://pn-network.net/sprites/tc_012_2.png",
"fileupload" => "http://pn-network.net/sprites/tc_012_2.png",
"MAX_FILE_SIZE" => "13145728",
"refer" => "",
"brand" => "",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/transload.php");
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
curl_close($ch);
?>

I also tried

<?php
$post_data = array(
"url" => "http://pn-network.net/sprites/tc_012_2.png",
"fileupload" => "http://pn-network.net/sprites/tc_012_2.png",
"MAX_FILE_SIZE" => "13145728",
"refer" => "",
"brand" => "",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/transload.php");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
curl_close($ch);
?>

and

<?php
$post_data = array(
"url" => "http://pn-network.net/sprites/tc_012_2.png",
"fileupload" => "http://pn-network.net/sprites/tc_012_2.png",
"MAX_FILE_SIZE" => "13145728",
"refer" => "",
"brand" => "",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.imageshack.us/transload.php");
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_exec($ch);
curl_close($ch);
?>

But so far no luck. Btw. I did try and send the post vars to another page and printed them. They came out fine.

Link to comment
Share on other sites

Not trying to knock you down for doing this, but odds are if this site is any bit respectable and this is not the intended use of this (cURL should only be used with permission from the end user/right to do it) and they will use a hidden captcha that is a session/value deal preventing you from submitting illegal forms.  Even if you can connect and so forth, their php/action script will die saying invalid session.

Link to comment
Share on other sites

Not trying to knock you down for doing this, but odds are if this site is any bit respectable and this is not the intended use of this (cURL should only be used with permission from the end user/right to do it) and they will use a hidden captcha that is a session/value deal preventing you from submitting illegal forms.  Even if you can connect and so forth, their php/action script will die saying invalid session.

umm. Well I didn't see anything wrong with doing this. Because the website allows users to host images for free (without requiring login). That's basically what I'm doing, storing images using php so I don't have to do it manually. But I guess you're right I will email them and seek permission anyways.

I got it to working though.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.