1

I'm new to curl. Just I try to post the value using curl script but I'm getting empty response. Help me is there any mistake in my code? How do I post a value using curl

$params = array('name' => 'karthick', 'type' => 'data'); 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php?action=create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
// curl_setopt($ch, CURLOPT_USERPWD,$authentication);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($ch, CURLOPT_REFERER,'http://www.example.com.au');
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 

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

var_dump($result);
2
  • are you getting the response status as 200 first? suggest setting verbose on and have you obtained a proper response from this URL previously, like using cURL from command prompt as there's a possibility that it might even be a server side issue? Commented Jun 6, 2012 at 12:17
  • got the response status -201 :) working now .Thanks for your valuable comments Commented Jun 6, 2012 at 14:02

3 Answers 3

1

You can try this code

public function getDataThroughCurlPost($param)
{ 
    $ch = curl_init("$url");
    error_reporting(E_ALL);
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, "$param");

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 0);

    $response = curl_exec($ch);
    $ch = curl_close("$url");
    return $response;
}
Sign up to request clarification or add additional context in comments.

3 Comments

this is the working code i am using this function, please send me your whole code
@KarthickV Can you plaese explain how it worked?? I am facing the same problem and am still not able to meke it work.
@Khaled Javeed - Just I have used $info = curl_getinfo($ch); var_dump($info);
0

Been using this for quite a few of my websites. Hope this helps.

$sPost .= "<Username>".$username."</Username>";
$sPost .= "<Password>".$password."</Password>";

$url = "YOUR_POST_URL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$logindetails = curl_exec($ch);
curl_close($ch);

$xmlarray = xml2array($details);

echo "<pre>"; 
print_r($xmlarray); 
echo "</pre>";

xml2array class found here: http://www.bin-co.com/php/scripts/xml2array/

1 Comment

Hmm..Use error_reporting(-1) to find out if there are any errors. Also have you got curl enabled?
0

// curl function starts

   function get_web_page( $url )
     {
             $options = array(
             CURLOPT_RETURNTRANSFER => true,     // return web page
             CURLOPT_HEADER         => false,    // don't return headers
             CURLOPT_FOLLOWLOCATION => true,     // follow redirects
             CURLOPT_ENCODING       => "",       // handle compressed
             CURLOPT_USERAGENT      => "spider", // who am i
             CURLOPT_AUTOREFERER    => true,     // set referer on redirect
             CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
             CURLOPT_TIMEOUT        => 120,      // timeout on response
             CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
             );
         $ch      = curl_init( $url );
         curl_setopt_array( $ch, $options );
     $content = curl_exec( $ch );
     $err     = curl_errno( $ch );
     $errmsg  = curl_error( $ch );
     $header  = curl_getinfo( $ch );
     curl_close( $ch );

     $header['errno']   = $err;
     $header['errmsg']  = $errmsg;
     $header['content'] = $content;
     return $header;
}

// curl function end

$cont = get_web_page("http://website.com/filename.php");

$handle = explode("<br>",$cont['content']);
print_r($handle);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.