tags:

views:

47

answers:

2
+3  Q: 

Post Curl issue

<?php
$url = "http://website.com/folder/index.php";
$data = array('id' => 'R98s', 'name' => 'Bob', 'content' => 'Hello');

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
?> 

This works great, only 1 problem though, id like a way to get the content response from the posted data in a variable, and not show as if its the page.

+2  A: 

Try the following:

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE ); // return into a variable
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $result = curl_exec( $ch ); // run!
        curl_close($ch);

And never forget the curl_close($handle); at the end.

Traveling Tech Guy
Beat me by 30 seconds. +1
Byron Whitlock
curl_close() probably happens by default when your script terminates, no? And possibly when the variable goes out of scope? Can you explain in more detail?
Frank Farmer
It has to do with on-time resource releasing and overall good programming: when you no longer need a file/handle/db connection - set it free.
Traveling Tech Guy
A: 

I always thought you needed this too

curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($handle)
alex