2013-10-18 75 views
0

我将在本网站上进一步使用PHP,否则有兴趣学习更多的python来实现这些结果。将PHP字符串从HTTP发布到另一个服务器

我从一个搜索表单开始,允许用户输入需要转换为url的'findme'值。 (例如目的,我将使用findme = 12345678)

<form name="search" method="post" action="search.php" target="_blank" novalidate> 
<input type="text" name="findme" /> 
<input type="submit" name="submit" value="submit" /> 
</form> 

然后,我想从第二服务器检索HTTP POST响应页面中的字符串和URL存储为一个PHP字符串。

首先,我需要将表格提交到另一台服务器,这是我在search.php中

<?php 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://another.server.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POST, true); 

$data = array(
    'surname' => 'surname', 
    'name' => 'name', 
    'findme' => 'findme' 
); 

curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
$output = curl_exec($ch); 
$info = curl_getinfo($ch); 
curl_close($ch); 
?> 

其他服务器会通过投放新的页面响应尝试(即https://another.server.com/response.html),我想然后找到包含findme字符串的行在下面是findmet值12345678将出现在响应页面的一行中的格式。我想将ABCDE保存为一个字符串。

<tr class="special"><td><a href="/ABCDE">12345678</a>...... 

希望我能与

<?php 
file_put_contents("response.html", file_get_contents("https://another.server.com/response.html")); 
$content = file_get_contents('response.html'); 
preg_match('~^(.*'.$findme.'.'</a>'.*)$~',$content,$line); 
echo $line[1]; 
$findme_url = substr("abcdef", -37, 5); 
echo $findme_url 
?> 

与卷曲更新acheive和的preg_match可能的解决方案,但该文件把内容需要从卷曲

读取响应页面
+0

卷曲似乎是一种选择;也许用ajax你也可以做到。 – Hackerman

+0

到目前为止,你有什么尝试?你不是要求为你编码整个事情,对吧?向我们展示您编写的一些代码。 –

+0

Hi @Jack,我对javascript onclick的第1步有一些想法,但是我打算继续使用cURL,我将尝试在线获得演示,以便我的代码可见并且问题更有意义。 – TinCan

回答

0

是的,这是一个完美的时间使用卷曲。

$request = curl_init('https://another.server.com'); 
curl_setopt($request, CURLOPT_POST, true); // use POST 
$response = curl_exec($request); 

// catch errors 
if($response === false) { 
    throw new Exception(curl_error($response)); 
} 

curl_close($request); 

// parse response... 
+0

谢谢,我会以此为出发点,看看我能得到多少 – TinCan

相关问题