2012-06-06 197 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