PHP cURL 发送到端口 8080
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6289878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP cURL sending to port 8080
提问by VeeBee
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 的站点进行 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);
am i missing something here?
我在这里错过了什么吗?
回答by SilentSteel
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 上的出站。
回答by Ohadsh
Simple CURL GET
request: (Also added json/headers if required, to make your life easier in need)
简单的 CURLGET
请求:(如果需要,还添加了 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);
?>
Charsetis 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_curl.dll
在您的 php 上启用了扩展,并且目标服务器上的端口也已打开。
Hope this helps.
希望这可以帮助。
回答by Scott Arciszewski
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.
如果不出意外,它将有助于解决您的问题并消除网络问题。
回答by David
First check that you're able to connect using something other than PHP's horrible Curl syntax:
首先检查您是否能够使用 PHP 可怕的 Curl 语法以外的其他内容进行连接:
- Chrome's Postman is easy to useif you're on your local machine,
- 如果您在本地机器上,Chrome 的 Postman 很容易使用,
else look at using (for linux users)
其他看看使用(对于linux用户)
- curl somesite:8080
- wget -qO- somesite:8080
- 卷曲某个站点: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");
回答by Daniel Convissor
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');
回答by vikashraj144
// 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');
回答by Hadrien Huvelle
Are you sure that the server you intent to join isn't firewalled? And that Selinux is disabled?
您确定要加入的服务器没有防火墙吗?那Selinux被禁用了?
回答by Chilion
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 中添加)
回答by Sunil Kumar Sain
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "your-domain-name");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch); `
?>
回答by khalrd
try this option for curl
尝试使用此选项进行卷曲
curl_setopt($ch, CURLOPT_PROXY,"localhost:8080");
or use this
或使用这个
curl_setopt($ch, CURLOPT_PROXY,"yourservername:8080");