简体   繁体   English

没有cURL的PHP​​4 HTTP发布

[英]PHP4 HTTP Post without cURL

I have the following code that works on PHP5 to send a HTTP POST without using cURL. 我有以下代码可在PHP5上运行,而无需使用cURL发送HTTP POST。 I would like this to work on PHP 4.3.0 and above: 我想在PHP 4.3.0及更高版本上工作:

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Type: application/json\r\n",
        'content' => $query
    )
);
$context  = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

HTTP context is only supported on PHP5. HTTP上下文仅在PHP5上受支持。 Is there anyway to make this work with PHP 4.3.0 - I need a fallback method if PHP5 or cURL is not installed. 无论如何,有没有要使它与PHP 4.3.0一起使用-如果未安装PHP5或cURL,我需要一个备用方法。

You shouldn't be using PHP 4. It is discontinued and so doesn't receive any security patches. 您不应该使用PHP4。它已经停产,因此不会收到任何安全补丁。

If you want to take a look writing something yourself, you want to start with fsockopen() . 如果您想看看自己写的东西,则想从fsockopen()开始。 There is a basic example that may do what you want and it shouldn't be too hard to convert it to a POST request. 有一个基本的示例可以满足您的要求,将它转换为POST请求并不难。 I used a packet sniffer (Wireshark) to get HTTP samples that I could (in essence) copy and paste into my PHP application however the specifications and a lot of samples are available online. 我使用数据包嗅探器(Wireshark)来获取HTTP示例,我可以(实质上)将其复制并粘贴到我的PHP应用程序中,但是该规范和许多示例可以在线获得。

Something like the following will do the trick if anyone needs help with this. 如果有人需要与此相关的帮助,则可以使用以下类似的技巧。

        $headers = "POST $url 1.1\r\n"; 
        $headers .= "Content-type: application/x-www-form-urlencoded\r\n";
        $headers .= "Content-Type: application/json\r\n";
        $headers .= "Accept: application/json\r\n";
        if (!empty($body)) {
            $headers .= "Content-length: " . strlen($body) . "\r\n";
        }
        $headers .= "\r\n";

        $body = $query;

        if ($fp = fsockopen($host, 80, $errno, $errstr, 180)) {
            fwrite($fp, $headers . $body, strlen($headers . $body));
            fclose();
        }

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

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