简体   繁体   English

如何通过在PHP中将POST参数发布到不同的网站来从绝对URL获取cURL响应?

[英]How to get cURL response from absolute URL by posting POST parameters to different website in PHP?

I want to get response from the URL http://webcache.gmc-uk.org/gmclrmp_enu/start.swe by sending some POST parameters into it. 我想通过发送一些POST参数从URL http://webcache.gmc-uk.org/gmclrmp_enu/start.swe获得响应。 I have written a function as follows: 我写了一个函数如下:

public function getResponse($params, $url)
{
    $post_data = '';
    foreach ($params as $key => $value) {
        $post_data .= $key . '=' . $value . '&';
    }

    //create the final string to be posted using implode()
    $post_data = rtrim($post_data, '&');

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, count($post_data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
}

But, when I call this function as: 但是,当我将此函数称为:

public function getIndex()
{
    $params = array(
        "s_3_1_5_0" => "5205500"
    );
    $url = 'http://webcache.gmc-uk.org/gmclrmp_enu/start.swe';
    echo $this->getResponse($params, $url);
}

I am always redirected to the page: http://localhost:8000/gmclrmp_enu/start.swe instead of getting response from the specified URL. 我总是被重定向到页面: http:// localhost:8000 / gmclrmp_enu / start.swe而不是从指定的URL获取响应。 where, http:// localhost:8000/ is root URL of my local project. 其中,http:// localhost:8000 /是我本地项目的根URL。

How could I get the response from that URL and dump it? 我如何从该URL获取响应并将其转储?

I tried your code and got this response. 我尝试了你的代码,得到了这个回应。

<html>
<body>
<form action="/gmclrmp_enu/start.swe" method="POST" name="RedirectForHost">
    <input type = "hidden" name="s_3_1_5_0" value="5205500">
    <input type = "hidden" name="SWEBHWND" value="">
    <input type = "hidden" name="_sn" value="QG3N3byeQ-lw2X0470Taoo2Mr1xU6fklSaK4bX.yXOH1DqcEpmRHVzctRxa1UYflcu-svwV7M0VFB6KvWmUEh4mUDGbJtaXeil1PnikFKVpr6fRP.i-GMhMRm41kZVFHaZA1QBjteOlfTXcwF0CLSh.MzwHUhdPVvYK9Ulfe.zCJQiSkU2XOt68YjT1lD-4jrTrIBzxJLUY_">
    <input type = "hidden" name="SRN" value="">
    <input type = "hidden" name="SWEHo" value="">
    <input type = "hidden" name="SWETS" value="1481276787">
</form>
<script language="javascript">
    var formObj = document.forms["RedirectForHost"];
    formObj.SWEHo.value=top.location.hostname;
    formObj.submit();
</script>
</body>
</html>

I think you got response like this, and then javascript submitted the form to localhost:8000. 我觉得你有这样的回应,然后javascript将表单提交给localhost:8000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM