2

我需要使用文件中的数据向外部源发出 POST 请求,所以我尝试合并我看到的这两个解决方案,但我似乎无法让它们工作,你能帮帮我吗?

// Submit those variables to the server
$post_data = array(
    'test' => 'foobar',
    'okay' => 'yes',
    'number' => 2
);

// Send a request to example.com 
$result = post_request('http://www.example.com/', $post_data);

if ($result['status'] == 'ok'){

    // Print headers 
    echo $result['header']; 

    echo '<hr />';

    // print the result of the whole request:
    echo $result['content'];

}
else {
    echo 'A error occured: ' . $result['error']; 
}

在 array() 我放

$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

所以最后我得到了这样的东西

<?php
    $post_data = array($trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);


    $result = post_request('http://www.example.com/', $post_data);

    if ($result['status'] == 'ok'){


        echo $result['header']; 

        echo '<hr />';


        echo $result['content'];

    }
    else {
        echo 'A error occured: ' . $result['error']; 
    }
?>

编辑:如果我不清楚我需要什么,我很抱歉。实际上,我偶然发现有人与我在这里 stackoverflow.com/questions/46582/response-redirect-with-post-instead-of-get 有相同的需求,尽管我想用 php 来做。

基本上,我被要求在网站内制作一个快速表单,用户在其中编写一些保存在 .txt 中的凭据,并且该数据用于通过 POST 登录到附属外部网站。这使用户能够从一开始就登录到附属网站,而无需单击新链接。

4

1 回答 1

0

您的代码的第一行有问题。第一个片段的数据属性也丢失了。您也可以添加它们。

$post_data = array($trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

将其替换为

$post_data = array(
    'trimmed' => file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
);

在 PHP 中声明数组见官方文档

如果您需要文件中的数据,您可能需要将其解析为数组,然后将其发布到 url。如果是这种情况,您可以发布txt文件的格式吗?

于 2013-03-11T13:19:25.383 回答