【问题标题】:Post request in PHP to form在 PHP 中发布请求以形成
【发布时间】:2012-10-26 09:00:21
【问题描述】:

我正在尝试向具有指向 tcl 脚本的表单的站点发送 POST 请求。 我可以在页面的源代码中看到 -

<FORM method=post action=<script>.tcl name=form1 

我尝试编写一个发送请求的 php 脚本

$url = '<url>/<script>.tcl';
$data = array('Username' => ... , 'Password' => ...);
$options = array('http' => array('method'  => 'POST','content' => http_build_query($data)));
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

但我收到:file_get_contents(..) - 无法打开流:HTTP 请求失败! HTTP/1.0 500 内部服务器错误

我是新来的,所以非常感谢您的帮助。 谢谢。

【问题讨论】:

  • 你需要使用 cURL。
  • 谢谢,我更改了它,现在我得到了 - 由于此服务器上的系统错误,无法访问请求的 URL。
  • 您的网址是否受到某种保护,例如 htaccess?

标签: php forms http post


【解决方案1】:

看起来像是cURL 的工作。

例如:

<?php

$url = '<url>/<script>.tcl';
$data = array('Username' => ... , 'Password' => ...);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// Return the response...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

curl_close ($ch);

echo $response;

【讨论】:

  • 我得到相同的结果 - 由于此服务器上的系统错误,无法访问请求的 URL。
  • 您的服务器是否安装了 curl? in_array('curl', get_loaded_extensions()) 你确定网址是正确的吗?
猜你喜欢
  • 1970-01-01
  • 2017-10-11
  • 2011-01-19
  • 1970-01-01
  • 2015-12-06
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
相关资源
最近更新 更多