Jump to content

Posting data using CURL


Pufas

Recommended Posts

Hi there,

OK, there is what I'm trying to accomplish - I want to send POST instantly when the page is loaded without "Submit" button or anything like that.

Here how it looks using JavaScript:

<form name="postdata" action="http://www.mywebsite.com/post.php" method="post">
    <input name="data" type="text" value="1"/>
</form>
<script type="text/javascript" language="JavaScript">
document.postdata.submit();
</script>

 

Works fine with JavaScript, however i can't use it.

 

Here is reference for CURL: http://php.net/manual/en/book.curl.php

I found this curl_post function at php.net to simplify things up.

<?php

/**
* Send a POST requst using cURL
* @param string $url to request
* @param array $post values to send
* @param array $options for cURL
* @return string
*/
function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}
$test = curl_post("http://www.mywebsite.com/post.php", array("data" => "1"));
print($test);
?>

 

Looks like it uses GET instead POST (used sniffer to verify this).

I'm trying to send POST data to other host, if that matters.

Any help would be appreciated.

Thanks in advance.

 

Link to comment
Share on other sites

I've found another function that does work:

<?php
$data = array();
$data['data'] = '1';


$post_str = '';
foreach($data as $key=>$val) {
$post_str .= $key.'='.urlencode($val).'&';
}
$post_str = substr($post_str, 0, -1);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mywebsite.com/post.php' );

curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$result = curl_exec($ch);
curl_close($ch);
echo $result;

?>

 

But once I got i running I found out it's not quiet what i wanted if the first place.

Using CURL, POST is send by the server (where this, where php script that's above is being hosted) to 'http://www.mywebsite.com/post.php'. But I want POST to be send by the user who browses my other web page (let's call it www.myfirstwebpage.com).

So, using curl i get this:

www.myfirstwebpage.com ---(POST)---> www.mywebsite.com

But that's what i want (i get it using JavaScript):

User ---(POST)---> www.mywebsite.com

 

So put it simple i want to User to POST "data=1" to www.mywebsite.com automatically once the page is loaded.

 

Is it possible to do this via php?

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.