1

我是新手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);
4

3 回答 3

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;
}
于 2012-06-06T10:52:38.193 回答
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/

于 2012-06-06T11:25:42.543 回答
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函数结束

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

$handle = explode("<br>",$cont['content']);
print_r($handle);
于 2012-06-06T13:21:50.560 回答