【问题标题】:PHP string from HTTP post to another server从 HTTP 发布到另一台服务器的 PHP 字符串
【发布时间】:2023-03-30 09:46:01
【问题描述】:

我将在这个网站上进一步使用 PHP,否则有兴趣学习更多 python 来实现这些结果。

我从一个搜索表单开始,该表单允许用户输入需要转换为 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 发布响应页面中的字符串,并将 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 字符串的行,下面是 findme 值 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
?>

更新了 cURL 和 preg_match 可能的解决方案,但是文件放置内容需要从 cURL 读取响应页面

【问题讨论】:

  • Curl 似乎是一种选择;也许使用 ajax 你也可以做到这一点。
  • 到目前为止,您尝试过什么?您不是要求为您编写整个代码,对吗?向我们展示您编写的一些代码。
  • 嗨@Jack,我对第 1 步使用 javascript onclick 有一些想法,但对使用 cURL 持开放态度,我将尝试在线获取演示,以便我的代码可见并且问题更有意义.

标签: php html post curl grep


【解决方案1】:

是的,这是使用 curl 的最佳时机。

$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... 

【讨论】:

  • 谢谢,我会以此为起点,看看我能走多远
猜你喜欢
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多