简体   繁体   English

PHP发送自定义HTTP请求

[英]PHP Send Custom HTTP Request

How can I send a custom HTTP Request to a server whose URL is "http://[IP]:[Port]/"? 如何将自定义HTTP请求发送到URL为“http:// [IP]:[端口] /”的服务器?

What I mean is that, instead of the first line being a normal GET or POST like so: 我的意思是,而不是第一行是正常的GET或POST,如下所示:

GET /index.html HTTP/1.1
Host: www.example.com

How can this be replaced with something just like: 如何将其替换为以下内容:

CUSTOM
Host: [IP]

I don't mind having to use any additional libraries if necessary such as cURL. 我不介意在必要时使用任何其他库,例如cURL。

UPDATE: 更新:

I've tried using the following cURL code: 我尝试使用以下cURL代码:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://[IP]:[Port]/");
curl_setopt($ch, CURLOPT_PORT, [Port]);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "CUSTOM");

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

print($output);

But it just keeps loading for 2 minutes until it said internal error (both with and without using the CURLOPT_CUSTOMREQUEST). 但它只是加载2分钟,直到它说内部错误(使用和不使用CURLOPT_CUSTOMREQUEST)。 However, if I use a standard website such as http://google.com it'll work fine. 但是,如果我使用标准网站(如http://google.com),它将正常运行。

Also I forgot to mention, the port my server is using is 7899, is this ok? 另外我忘了提一下,我服务器使用的端口是7899,这样可以吗? Open it in my web browser fine, but cURL doesn't seem to be able to. 在我的网络浏览器中打开它很好,但cURL似乎无法做到。

Looks like there's nothing wrong with your code. 看起来您的代码没有任何问题。 If you're using a shared hosting provider, ask them to open up outbound TCP port 7899. 如果您使用的是共享主机提供商,请让他们打开出站TCP端口7899。

You can create a custom HTTP request method using the http_request_method_register function: 您可以使用http_request_method_register函数创建自定义HTTP请求方法:

http://www.php.net/manual/en/function.http-request-method-register.php http://www.php.net/manual/en/function.http-request-method-register.php

This code needs to be run on the server that will be handling the request. 此代码需要在将处理请求的服务器上运行。 If you try to use a non-standard HTTP request method on any old server, depending on the server it may ignore it, or may return an error code. 如果您尝试在任何旧服务器上使用非标准HTTP请求方法,则可能会忽略它,或者可能返回错误代码。

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

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