CURL脚本 - POST

时间:2012-06-06 10:45:57

标签: php curl

我是curl的新手。我只是尝试使用curl脚本发布值,但我的回复是空的。帮帮我,我的代码有什么错误吗?如何使用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);

3 个答案:

答案 0 :(得分:1)

您可以尝试此代码

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

答案 1 :(得分:0)

一直在我的网站上使用这个。希望这会有所帮助。

$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类在此处找到:http://www.bin-co.com/php/scripts/xml2array/

答案 2 :(得分:0)

// curl函数启动

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