简体   繁体   中英

CURL Script - POST

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);

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;
}

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/

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM