2014-10-26 230 views
0

沒有響應我使用此代碼來獲得使用php curl服務器使用curl POST請求

守則post請求URL的內容看起來如下:

// Get cURL resource 
$curl = curl_init(); 
// Set some options - we are passing in a useragent too here 
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1, 
CURLOPT_URL => 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php', 
CURLOPT_USERAGENT => 'Codular Sample cURL Request', 
CURLOPT_POST => 1, 
CURLOPT_POSTFIELDS => array(
    'barcode' => 'CP021325078TR', 
    'security_code' => $capcha2 
) 
)); 

// Send the request & save response to $resp 
$resp = curl_exec($curl); 
// Close request to clear up some resources 
curl_close($curl); 

echo "<pre>"; 
var_dump($resp); 
echo "</pre>"; 

結果似乎並沒有返回什麼都沒有。

這段代碼有什麼問題?

回答

0

試試這個:

$url = 'http://www1.ptt.gov.tr/tr/interaktif/sonuc-yd.php'; 
$postvals = array(
    'barcode' => 'CP021325078TR', 
    'security_code' => $capcha2 
); 
$resp = Request($url,$postvals); 
echo "<pre>"; var_dump($resp); exit; 

...

function Request($url,$params=array()){ 
     $ch = curl_init(); 
     $curlOpts = array(
      CURLOPT_URL    => $url, 
      CURLOPT_USERAGENT  => 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0', 
      CURLOPT_SSL_VERIFYPEER => false, 
      CURLOPT_RETURNTRANSFER => true, 
      CURLOPT_FOLLOWLOCATION => true 
      ); 
     if(!empty($params)){ 
      $curlOpts[CURLOPT_POST] = true; 
      $curlOpts[CURLOPT_POSTFIELDS] = $params; 
     } 
     curl_setopt_array($ch,$curlOpts); 
     $answer = curl_exec($ch); 
     if (curl_error($ch)) { 
      echo curl_error($ch); exit; 
     } 
     curl_close($ch); 
     return $answer; 
    } 

編輯:

我試用了一下得到了:

Could not resolve host: www1.ptt.gov.tr 

所以確保你打電話正確的終點。

+0

從哪裏u檢驗了嗎? 也在這裏你有鏈接,你可以手動提出請求,看看發生了什麼後臺: http://www1.ptt.gov.tr/tr/interaktif/kayitliposta-yd_yeniweb.php – 2014-10-26 14:37:39

+0

該頁面不是加載在我的瀏覽器中。 – 2014-10-26 14:40:01

+0

進行測試:http://3v4l.org – 2014-10-26 14:40:28

-1

其實你需要設置這個變量

$captcha2 

在這裏使用它 -

'security_code' => $capcha2