【问题标题】:POST request with data file带有数据文件的 POST 请求
【发布时间】:2013-02-26 15:11:32
【问题描述】:

我需要使用文件中的数据向外部源发出 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 登录到附属的外部网站。这使用户能够从一开始就登录到附属网站,而无需单击新链接。

【问题讨论】:

标签: php file post text


【解决方案1】:

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

$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)
);

See the official documentation for declaring arrays in PHP

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

【讨论】:

  • 感谢您的所有帮助,虽然我未能通过 post 实现目标,但事实证明,附属网站表单接受通过 GET 登录,因此我使用简单的 GET 重定向实现了我的目标。我感谢您的帮助。干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 1970-01-01
相关资源
最近更新 更多