2012-06-06 155 views
1

我是新来curl。只是我尝试使用curl脚本发布价值,但我得到空的答复。帮助我在我的代码中有任何错误?如何使用curl卷曲脚本 - POST

$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); 
+0

你得到200首先响应状态?建议详细的设置和你获取此URL正确的响应之前,喜欢用卷曲从命令提示符有一个可能性,它甚至可能是一个服务器端的问题? – optimusprime619

+0

得到响应状态-201 :)现在的工作.Thanks您的宝贵意见 –

回答

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

您好我试过,但没有得到结果 –

+0

这是我使用该功能的工作代码,请给我你的整个代码 – 2012-06-08 09:56:11

+0

Anoop :)感谢其工作现在 –

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/

+0

您好我想你的代码仍然我得到空响应 –

+0

Hmm..Use的error_reporting(-1),以找出是否有任何错误。你也有卷曲启用? – Dev

0

//卷曲功能开始

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

//卷曲功能结束

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

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

这不POST – Leigh