PHP cURL 发送到端口 8080

问题描述 投票:0回答:10

今天我尝试对某个正在侦听端口 8080 的站点进行curl 调用。但是,这样的调用会发送到端口 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);

我在这里错过了什么吗?

php curl port
10个回答
4
投票

请注意,我以前遇到过此问题,因为我使用的 Web 主机阻止了标准 80 和 443 (HTTPS) 之外的出站端口。因此,您可能想询问他们或进行一般测试。事实上,为了安全起见,一些主机甚至经常阻止端口 80 上的出站。


1
投票

简单的 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);
?>

示例

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不是必需的HTTP标头,其他也不是必需的,重要的是Content-Type

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

确保您的 php 上启用了

php_curl.dll
扩展,并且目标服务器上的端口已打开。

希望这有帮助。


0
投票

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

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

  • 卷曲某个网站:8080
  • wget -qO- 某个站点:8080

一旦确定可以连接,那么您就可以开始使用 PHP Curl 的可怕业务了。有包装纸,但我发现它们很薄。

Curl、Wget 或类似工具都可以非常轻松地配置为使用 GET 和 POST 方法。这不是可取的,但对于使用 Curl 进行的不止一项复杂操作,我只是放弃了正确配置 PHP 库的尝试,而是简单地使用了命令行。

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

<? 

//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");

0
投票

您的网络服务器配置错误。您提供的代码对我有用。

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

CURLOPT_PORT
CURLOPT_URL
行即可:

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


0
投票

也许你可以使用 --本地端口 [-num]

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

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


0
投票

使用 PHP cURL 向服务器上的 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');

-1
投票

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

sh
telnet somesite.tld 8080

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


-1
投票

尝试使用此选项进行卷曲

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

或使用这个

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

-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
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.