将 CLIcurl 命令翻译成 PHP

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

我有以下curl命令(更改数据以保护无辜者:

curl -s -i -k -X POST https://myserver.mydomain.com/rest/login -H 'Content-Type: application/json' -d '{"username": "myuser","password":"mypass","domain":"mydomain"}'

这在命令行中运行得很好,我得到了适当的响应。

我尝试使用以下代码将其转换为 PHP 卷曲请求:

$ch = curl_init();
$postFields = json_encode([
    "username"=>$myuser,
    "password"=>$mypass,
    "domain"=>$mydomain
]);

curl_setopt($ch, CURLOPT_URL, "https://" . $myserver . "/rest/login");
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$webresponse = curl_exec($ch);
$info = curl_getinfo($ch);
print_r($webresponse);
print_r($info);

但是,当我运行此命令时,我没有从网络响应中得到任何信息,并且 $info 显示:

Array
(
    [url] => https://myserver.domain.com/rest/login
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.007508
    [namelookup_time] => 0.008522
    [connect_time] => 0
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 
    [certinfo] => Array
        (
        )

    [primary_port] => 0
    [local_ip] => 
    [local_port] => 0
    [http_version] => 0
    [protocol] => 0
    [ssl_verifyresult] => 0
    [scheme] => 
    [appconnect_time_us] => 0
    [connect_time_us] => 0
    [namelookup_time_us] => 8522
    [pretransfer_time_us] => 0
    [redirect_time_us] => 0
    [starttransfer_time_us] => 0
    [total_time_us] => 7508
)

任何人可以提供的任何帮助将不胜感激。

php curl
1个回答
0
投票

事实证明,这是一个 selinux 问题,而不是 PHP 问题。 不允许 Web 服务器发出外部 HTTP 请求。 一旦我使用“setsebool -P httpd_can_network_connect on”启用该功能,我就能够获取要通过的请求。

感谢所有试图提供帮助的人。

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