使用PHP发送POST请求 - 失败

时间:2017-01-16 22:15:10

标签: php post

我一直在寻找一种方法来发送带有PHP标题和内容的POST请求,我在stackoverflow上找到了一个很好的解决方案,这里是代码:

<?php
$uid1 = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 8)), 0, 8);
$uid2 = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 4)), 0, 4);
$uid3 = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 4)), 0, 4);
$uid4 = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 4)), 0, 4);
$uid5 = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyz", 12)), 0, 12);
$uid6 = substr(str_shuffle(str_repeat("0123456789", 1)), 0, 1);
$randuid = ("demo."."00000000"."-".$uid2."-".$uid3."-".$uid4."-".$uid5);
$url = 'https://www.something.com/rest/client/users/ticket/demo';
$data = array('password' => 'demo', 'deviceUid' => $randuid, 'deviceModelId' => '14');
$options = array(
        'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    )
);

$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);

?>

我得到了非常好的回应,但是当我第二次尝试时,我什么都没得到。当我更改我的IP地址时,我再次给出肯定的回复,然后再次第二次它不会工作。 它给了我以下错误:

  

无法打开流:HTTP请求失败! HTTP / 1.1 503服务暂时&gt; &GT;不可用

但是当我在http://requestmaker.com/上发布请求时 我在这个网站上发出了1000多个请求,每次都能完美无缺地运行。 这家伙做的很棒,据我所知,这个文件正在发送请求 http://requestmaker.com/requester.php但没有更多信息。 他不是每次都从新的IP地址发送请求,因为这是不可能的。 这个开发者的秘诀是什么?

1 个答案:

答案 0 :(得分:0)

503表示功能正在运行,但您从服务器获取拒绝访问的响应。如果您尝试过cURL谷歌搜索结果,则会发生相同的事情,因为他们可以检测到file_get_contents和cURL使用的用户代理,从而阻止这些用户代理。

这些命令不起作用的主要原因主要有三个:

  1. 默认的USER-AGENT已被阻止。
  2. 您的服务器IP块已被阻止。
  3. 远程主机具有代理检测功能。
  4. 我也遇到过这样的情况,但我所做的只是添加User-Agent标题并将其设置为我正在使用的浏览器的用户代理。例如:

    Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36
    

    这可能有效:

    $options = array(
            'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded; charset=UTF-8\r\n; User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
            'method'  => 'POST',
            'content' => http_build_query($data),
        )
    );