【问题标题】:POST request from php来自 php 的 POST 请求
【发布时间】:2012-12-14 01:46:03
【问题描述】:

我需要一个 php 页面,在加载时自动发送 POST 请求,而无需使用提交按钮或任何用户输入。我不需要对响应做任何事情。

我只需要将“8”发布到 www.mydomain.com

$url = 'http://www.mydomain.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 8);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

我做错了什么?

【问题讨论】:

    标签: php post curl


    【解决方案1】:

    试试这个:

    curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 8));
    

    在您的域编辑脚本上:

    echo $_POST['data'];
    

    【讨论】:

    • 还是不行。我现在有: $url = 'mydomain.com' $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 8)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch);
    • 我试试$url = 'http://www.mydomain.com/'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 8)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); echo $response = curl_exec($ch); curl_close($ch); 对我来说效果很好
    • 谢谢。我会投赞成票,但我的代表不够好......就像在高中一样。
    【解决方案2】:

    有点奇怪的请求..您应该考虑使用值 8 的键。

    但是,您可以通过阅读 'php://input' 来获取 http://www.mydomain.com/ 上的 POST 正文。

    //on www.mydomain.com/index.php
    echo file_get_contents('php://input'); //8
    

    顺便说一句,您在第三个 setopt 上还有 $curl 而不是 $ch

    【讨论】:

    • file_get_contents 发送GET 请求
    • @Charles-EdouardCoste 我假设你的意思是file_get_contents('http://www.mydomain.com/') 会发送一个 GET 请求。这不是我所建议的。请仔细阅读我的回答;如果您想尝试一下,请自己测试一下
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-03-19
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 2019-05-10
    相关资源
    最近更新 更多