PHP cURL发送到端口8080

时间:2022-12-07 18:13:43

today I am trying to make a curl call to somesite which is listening to port 8080. However, calls like this get sent to port 80 and not 8080 instead:

今天我正在尝试对正在侦听端口8080的某个站点进行卷曲调用。但是,这样的调用会被发送到端口80而不是8080:

$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

am i missing something here?

我在这里错过了什么吗?

10 个解决方案

#1


2  

Just a note, I've had this issue before because the web host I was using was blocking outbound ports aside from the standard 80 and 443 (HTTPS). So you might want to ask them or do general tests. In fact, some hosts often even block outbound on port 80 for security.

请注意,之前我遇到过这个问题,因为我使用的网络主机阻止了标准80和443(HTTPS)之外的出站端口。所以你可能想问他们或做一般测试。实际上,为了安全起见,一些主机甚至经常在端口80上阻止出站。

#2


1  

// Use PHP cURL to make a request to port 8080 on a server

$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');


PHP cURL sending to port 8080
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');

#3


0  

Have you tried to SSH into the server that runs this and verify the iptables rules, and/or attempt to connect via telnet to the target server?

您是否尝试通过SSH连接到运行此命令的服务器并验证iptables规则,和/或尝试通过telnet连接到目标服务器?

sh telnet somesite.tld 8080

sh telnet somesite.tld 8080

If nothing else, it will help troubleshoot your problem and eliminate network issues.

如果不出意外,它将有助于解决您的问题并消除网络问题。

#4


0  

First check that you're able to connect using something other than PHP's horrible Curl syntax:

首先检查您是否能够使用PHP的可怕Curl语法之外的其他东西进行连接:

else look at using (for linux users)

否则看看使用(对于linux用户)

  • curl somesite:8080
  • wget -qO- somesite:8080
  • wget -qO- somesite:8080

Once you've established you can connect, then you can go about the horrible business of using PHP Curl. There are wrappers, but they're flaky that I've found.

一旦你建立起来就可以连接,那么你就可以开始使用PHP Curl了。有包装,但它们是我发现的片状。

Both Curl, Wget or similar can be very easily configured to use GET and POST methods. It's not advisible, but for more than one complicated operation using Curl I've simply given up trying to configure PHP's library correctly and simply dropped to the command line.

Curl,Wget或类似的都可以非常容易地配置为使用GET和POST方法。这是不可取的,但对于使用Curl的多个复杂操作,我只是放弃了尝试正确配置PHP库并简单地删除到命令行。

THERE ARE SECURITY IMPLICATIONS. You need to take great care to ensure that anything you give it, particularly if it's from a form or an external source, is appropriately escaped.

有安全隐患。您需要非常小心,确保您提供的任何内容,特别是来自表单或外部源的内容都适当地转义。

<? 

//Strip out any possible non-alpha-numeric character for security

$stringToSend = preg_replace('[^a-zA-Z]', '', $stringToSend);

$return = shell_exec("curl -X POST -d $stringToSend http://example.com/path/to/resource");

#5


0  

Your web server is misconfigured. The code you provided works for me.

您的Web服务器配置错误。您提供的代码适合我。

Also, your code can be simpler. Just put the URI into the init call and drop the CURLOPT_PORT and CURLOPT_URL lines:

此外,您的代码可以更简单。只需将URI放入init调用并删除CURLOPT_PORT和CURLOPT_URL行:

$ch = curl_init('http://somesite.tld:8080');

$ ch = curl_init('http://somesite.tld:8080');

#6


0  

Are you sure that the server you intent to join isn't firewalled? And that Selinux is disabled?

您确定要加入的服务器没有防火墙吗? Selinux被禁用了吗?

#7


0  

Maby you can use --local-port [-num]

Maby你可以使用--local-port [-num]

Set a preferred number or range of local port numbers to use for the connection(s). Note that port numbers by nature are a scarce resource that will be busy at times so setting this range to something too narrow might cause unnecessary connection setup failures. (Added in 7.15.2)

设置用于连接的本地端口号的首选数量或范围。请注意,端口号本质上是一种稀缺资源,有时会很忙,因此将此范围设置得太窄可能会导致不必要的连接设置失败。 (在7.15.2中添加)

Source: http://curl.haxx.se/docs/manpage.html#--ftp-pasv

#8


0  

Simple CURL GET request: (Also added json/headers if required, to make your life easier in need)

简单的CURL GET请求:(如果需要,还添加json / headers,以使您的生活更轻松)

<?php
$chTest = curl_init();
curl_setopt($chTest, CURLOPT_URL, "http://example.com:8080/?query=Hello+World");
curl_setopt($chTest, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($chTest, CURLOPT_HEADER, true);
curl_setopt($chTest, CURLOPT_RETURNTRANSFER, true);

$curl_res_test = curl_exec($chTest);
$json_res_test = explode("\r\n", $curl_res_test);

$codeTest = curl_getinfo($chTest, CURLINFO_HTTP_CODE);
curl_close($chTest);
?>

Example POST request:

示例POST请求:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com:8080');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Hello" : "World", "Foo": "World"}');
// Set timeout to close connection. Just for not waiting long.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$curl_res = curl_exec($ch);
?>

Charset is not a required HTTP header and so are the others, the important one is Content-Type.

Charset不是必需的HTTP头,其他的也是如此,重要的是Content-Type。

For the examples I used JSON MIME type, You may use whatever you want, take a look at the link: http://en.wikipedia.org/wiki/Internet_media_type

对于我使用JSON MIME类型的示例,您可以使用您想要的任何内容,请查看链接:http://en.wikipedia.org/wiki/Internet_media_type

Make sure that the php_curl.dll extension is enabled on your php, and also that the ports are open on the target serve.

确保在php上启用了php_curl.dll扩展,并且还在目标服务上打开了端口。

Hope this helps.

希望这可以帮助。

#9


-1  

try this option for curl

尝试这个选项卷曲

curl_setopt($ch, CURLOPT_PROXY,"localhost:8080");

or use this

或使用此

curl_setopt($ch, CURLOPT_PROXY,"yourservername:8080");

#10


-1  

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your-domain-name");
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); `
?>

#1


2  

Just a note, I've had this issue before because the web host I was using was blocking outbound ports aside from the standard 80 and 443 (HTTPS). So you might want to ask them or do general tests. In fact, some hosts often even block outbound on port 80 for security.

请注意,之前我遇到过这个问题,因为我使用的网络主机阻止了标准80和443(HTTPS)之外的出站端口。所以你可能想问他们或做一般测试。实际上,为了安全起见,一些主机甚至经常在端口80上阻止出站。

#2


1  

// Use PHP cURL to make a request to port 8080 on a server

$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');


PHP cURL sending to port 8080
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$target_response = curl_exec($ch);
curl_close($ch);

curl_setopt($ch, CURLOPT_URL, 'http://somesite.tld:8080');

#3


0  

Have you tried to SSH into the server that runs this and verify the iptables rules, and/or attempt to connect via telnet to the target server?

您是否尝试通过SSH连接到运行此命令的服务器并验证iptables规则,和/或尝试通过telnet连接到目标服务器?

sh telnet somesite.tld 8080

sh telnet somesite.tld 8080

If nothing else, it will help troubleshoot your problem and eliminate network issues.

如果不出意外,它将有助于解决您的问题并消除网络问题。

#4


0  

First check that you're able to connect using something other than PHP's horrible Curl syntax:

首先检查您是否能够使用PHP的可怕Curl语法之外的其他东西进行连接:

else look at using (for linux users)

否则看看使用(对于linux用户)

  • curl somesite:8080
  • wget -qO- somesite:8080
  • wget -qO- somesite:8080

Once you've established you can connect, then you can go about the horrible business of using PHP Curl. There are wrappers, but they're flaky that I've found.

一旦你建立起来就可以连接,那么你就可以开始使用PHP Curl了。有包装,但它们是我发现的片状。

Both Curl, Wget or similar can be very easily configured to use GET and POST methods. It's not advisible, but for more than one complicated operation using Curl I've simply given up trying to configure PHP's library correctly and simply dropped to the command line.

Curl,Wget或类似的都可以非常容易地配置为使用GET和POST方法。这是不可取的,但对于使用Curl的多个复杂操作,我只是放弃了尝试正确配置PHP库并简单地删除到命令行。

THERE ARE SECURITY IMPLICATIONS. You need to take great care to ensure that anything you give it, particularly if it's from a form or an external source, is appropriately escaped.

有安全隐患。您需要非常小心,确保您提供的任何内容,特别是来自表单或外部源的内容都适当地转义。

<? 

//Strip out any possible non-alpha-numeric character for security

$stringToSend = preg_replace('[^a-zA-Z]', '', $stringToSend);

$return = shell_exec("curl -X POST -d $stringToSend http://example.com/path/to/resource");

#5


0  

Your web server is misconfigured. The code you provided works for me.

您的Web服务器配置错误。您提供的代码适合我。

Also, your code can be simpler. Just put the URI into the init call and drop the CURLOPT_PORT and CURLOPT_URL lines:

此外,您的代码可以更简单。只需将URI放入init调用并删除CURLOPT_PORT和CURLOPT_URL行:

$ch = curl_init('http://somesite.tld:8080');

$ ch = curl_init('http://somesite.tld:8080');

#6


0  

Are you sure that the server you intent to join isn't firewalled? And that Selinux is disabled?

您确定要加入的服务器没有防火墙吗? Selinux被禁用了吗?

#7


0  

Maby you can use --local-port [-num]

Maby你可以使用--local-port [-num]

Set a preferred number or range of local port numbers to use for the connection(s). Note that port numbers by nature are a scarce resource that will be busy at times so setting this range to something too narrow might cause unnecessary connection setup failures. (Added in 7.15.2)

设置用于连接的本地端口号的首选数量或范围。请注意,端口号本质上是一种稀缺资源,有时会很忙,因此将此范围设置得太窄可能会导致不必要的连接设置失败。 (在7.15.2中添加)

Source: http://curl.haxx.se/docs/manpage.html#--ftp-pasv

#8


0  

Simple CURL GET request: (Also added json/headers if required, to make your life easier in need)

简单的CURL GET请求:(如果需要,还添加json / headers,以使您的生活更轻松)

<?php
$chTest = curl_init();
curl_setopt($chTest, CURLOPT_URL, "http://example.com:8080/?query=Hello+World");
curl_setopt($chTest, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($chTest, CURLOPT_HEADER, true);
curl_setopt($chTest, CURLOPT_RETURNTRANSFER, true);

$curl_res_test = curl_exec($chTest);
$json_res_test = explode("\r\n", $curl_res_test);

$codeTest = curl_getinfo($chTest, CURLINFO_HTTP_CODE);
curl_close($chTest);
?>

Example POST request:

示例POST请求:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com:8080');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"Hello" : "World", "Foo": "World"}');
// Set timeout to close connection. Just for not waiting long.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$curl_res = curl_exec($ch);
?>

Charset is not a required HTTP header and so are the others, the important one is Content-Type.

Charset不是必需的HTTP头,其他的也是如此,重要的是Content-Type。

For the examples I used JSON MIME type, You may use whatever you want, take a look at the link: http://en.wikipedia.org/wiki/Internet_media_type

对于我使用JSON MIME类型的示例,您可以使用您想要的任何内容,请查看链接:http://en.wikipedia.org/wiki/Internet_media_type

Make sure that the php_curl.dll extension is enabled on your php, and also that the ports are open on the target serve.

确保在php上启用了php_curl.dll扩展,并且还在目标服务上打开了端口。

Hope this helps.

希望这可以帮助。

#9


-1  

try this option for curl

尝试这个选项卷曲

curl_setopt($ch, CURLOPT_PROXY,"localhost:8080");

or use this

或使用此

curl_setopt($ch, CURLOPT_PROXY,"yourservername:8080");

#10


-1  

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your-domain-name");
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_exec($ch); 
curl_close($ch); `
?>