【问题标题】:PHP post requestPHP 发布请求
【发布时间】:2014-12-02 10:48:18
【问题描述】:

我必须使用 PHP 发布请求,但我无法使其工作。低 html 请求有效

<form action="http://example.com/form.php" method="post" id="form-success">
<input type="hidden" name="id" id="itemid" value="12345">
<input type="hidden" name="key" id="sh" value="79c830e5bf78218685a350cd5df3cdac">
<input type="submit"/>
</form>

我尝试了这两种在php中请求的方法,但都不起作用。

$content = http_build_query(array('id'=>'12345','key'=>'79c830e5bf78218685a350cd5df3cdac'));

$context = stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'content' => $content,
    )
));

$result = file_get_contents('http://example.com/form.php', null, $context);

echo $result;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/form.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('id'=>'12345','key'=>'79c830e5bf78218685a350cd5df3cdac');

$pagina = curl_exec ($ch);

echo $pagina;

没有错误信息,只知道页面没有正确重定向。 有任何想法吗? 谢谢你

【问题讨论】:

  • 可能是服务器阻止了一些用户代理(或没有用户代理字符串)。尝试将用户代理字符串添加到 curl davidwalsh.name/set-user-agent-php-curl-spoof
  • 当您提交表单时,您将被重定向到 form.php,您的数据位于 $_POST 中。究竟是什么,不起作用?你想达到什么目的。
  • 更改用户代理不起作用!
  • 我必须从客户端将表单提交到页面,该页面必须处理两个变量并重定向到另一个页面,当她由于某种原因没有处理相关信息时,它不会重定向页面。
  • Beauty 使用用户代理“Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / Safari 39.0.2171.71 / 537.36”谢谢@logic-unit跨度>

标签: php post curl httprequest


【解决方案1】:

我用这个:

function file_get_contents_post($url, $var_post = array()) {
    define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
    $header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
    $content = '' ;

    foreach ( $var_post as $key => $value ) {
        $content .= "--".MULTIPART_BOUNDARY."\r\n".
                    "Content-Disposition: form-data; name=\"".$key."\"\r\n\r\n".
                    $value."\r\n";
    }

    $content .= "--".MULTIPART_BOUNDARY."--\r\n";

    $context = stream_context_create(array(
        'http' => array(
              'method' => 'POST',
              'header' => $header,
              'content' => $content,
        )
    ));
    return file_get_contents($url, false, $context) ;
}

【讨论】:

    猜你喜欢
    • 2015-12-06
    • 2022-11-29
    • 2011-01-19
    • 1970-01-01
    • 2017-11-21
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多