2015-08-23 31 views
0


我已閱讀許多文章和討論(也在Stackoverflow中),但我還沒有找到解決方案。
我有一個外部網站的行動表單。
我需要提交我的表單獲取該網站的響應(我的http請求的響應頭是好的)。可能嗎?
我試圖用捲曲(CURLOPT_RETURNTRANSFER真),但也許有一些錯誤的,我下面的代碼:PHP POST數據和檢索響應與捲曲

 $post_data['xx'] = 'xxx'; 
     $post_data['yy'] = 'yyy'; 
     $post_data['zz'] = '13-12-2015'; 

     $postData = ''; 
     foreach($post_data as $k => $v) 
     { 
      $postData .= $k . '='.$v.'&'; 
     } 
     rtrim($postData, '&'); 

     $url = 'http://......'; 

     $ch = curl_init(); 

     curl_setopt($ch, CURLOPT_URL,$url); 
     curl_setopt($ch, CURLOPT_POST, 1); 
     curl_setopt($ch, CURLOPT_POSTFIELDS,$postData); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     $result = curl_exec ($ch);   
     $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     echo $http; 

     curl_close ($ch); 

返回爲「0」。

謝謝!

+0

什麼是不工作? '的var_dump($ HTTP)'? – Devon

+0

我有0作爲$ http響應。有任何想法嗎。我嘗試過使用var_dump,但收到「int 0」。 @Devon – Gio

+0

運行'print_r(curl_getinfo($ ch));'獲取更多信息,可以幫助您解決問題。 – Emil

回答

0

如果我拿起你在找什麼,讓我想此解決方案:

Main.php

<form method=post action=target.php><input name=mypostdata type=text ><input type=submit ></form><?php`include("curl.php");?> 

Target.php

<?php if(isset($_POST['mypostdata'])){echo "<span>done</span>";}else{}?> 

捲曲.php

<?php function file_get_contents_curl($url){ 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
$data = curl_exec($ch); 
curl_close($ch); 
return $data; 
    } 
$html = file_get_contents_curl("target.php"); 
$doc = new DOMDocument(); 
@$doc->loadHTML($html); 
$nodes = $doc->getElementsByTagName('span'); 
//get and display what you need: 
$response = $nodes->item(0)->nodeValue; 
echo $response; 
?>