简体   繁体   English

PHP字符串从HTTP发布到另一台服务器

[英]PHP string from HTTP post to another server

I will be using PHP further on this site, otherwise interested in learning more python to achieve these results. 我将在此站点上进一步使用PHP,否则有兴趣学习更多python以获得这些结果。

I start with a search form that allows the user to enter in the 'findme' value which needs to be translated to a url. 我从一个搜索表单开始,该表单允许用户输入需要转换为URL的“ findme”值。 (for example purposes I will use findme = 12345678) (出于示例目的,我将使用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>

And then, I would like to retrieve a string within a HTTP post response page from a second server and store a url as a PHP string. 然后,我想从第二台服务器检索HTTP后响应页面中的字符串,并将URL存储为PHP字符串。

First I need to submit the form to another server, here is my attempt at search.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);
?>

The other server responds by serving a new page (ie. https://another.server.com/response.html ), I want to then find the line containing the findme string, below is the format which the findme value of 12345678 would appear in a line of the response page. 另一台服务器通过提供一个新页面来响应(即https://another.server.com/response.html ),然后我想找到包含findme字符串的行,以下是findme值12345678的格式出现在响应页面的一行中。 I want to save ABCDE as a string. 我想将ABCDE保存为字符串。

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

Hopefully I can acheive with 希望我能与

<?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
?>

Updated with cURL and preg_match possible solutions, however the file put contents needs to be reading the response page from cURL 更新了cURL和preg_match可能的解决方案,但是文件放置内容需要从cURL读取响应页面

Yes, this is a perfect time to use 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... 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM