PHP-CLI работает с curl_init, но не с file_get_contents, как включить?Php

Кемеровские программисты php общаются здесь
Ответить
Anonymous
 PHP-CLI работает с curl_init, но не с file_get_contents, как включить?

Сообщение Anonymous »

Использование php -a

Код: Выделить всё

$url = "https://getcomposer.org/versions";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0");
echo curl_exec($ch);
< /code>
он работает нормально, возвращает строку JSON из URL. Теперь продолжение в PHP -A 
взаимодействие терминала:

Код: Выделить всё

echo file_get_contents($url);
... после долгого ожидания, скажем: "ПРЕДУПРЕЖДЕНИЕ PHP: file_get_contents (...): Не удалось открыть потоки: подключение, временное в коде оболочки PHP" .

Код: Выделить всё

error_reporting(E_ALL);
ini_set('display_errors', 1);

ini_set("auto_detect_line_endings","0");
ini_set("allow_url_fopen", "1");
ini_set("allow_url_include", "0");
ini_set("user_agent", "Mozilla/5.0");

echo file_get_contents($url);

// ---------------------
// Same (not work!) when using $context, as @sammitch's first try-answer:
$opts = [
'http' => [ 'user_agent' => 'Mozilla/5.0' ],
'https' => [ 'user_agent' => 'Mozilla/5.0' ]
];
$context = stream_context_create($opts);

echo file_get_contents($url,false,$context);

< /code>

ps: мне нужен file_get_contents для WordPress, композитора и т. Д. Этот вопрос о «почему file_get_contents не работает».php -v
PHP 8.0.30 (cli) (built: May 20 2025 13:31:19) ( NTS gcc x86_64 )
cat /etc/oracle-release
Oracle Linux Server release 9.6
< /code>
Подробнее от php -i | grep 
:

Код: Выделить всё

((grep -i "http"))
Registered PHP Streams => https, ftps, compress.zlib, php, file, glob, data, http, ftp, compress.bzip2, phar, zip
HTTP2 => Yes
HTTPS_PROXY => Yes
Protocols => dict, file, ftp, ftps, gopher, gophers, http, https, imap, imaps, ldap, ldaps, mqtt, pop3, pop3s, rtsp, scp, sftp, smb, smbs, smtp, smtps, telnet, tftp
HTTP input encoding translation => disabled
mbstring.http_input => no value => no value
mbstring.http_output => no value => no value
mbstring.http_output_conv_mimetypes => ^(text/|application/xhtml\+xml) => ^(text/|application/xhtml\+xml)
session.cookie_httponly => no value => no value
https_proxy => http://proxy.myIntranet:8080
http_proxy => http://proxy.myIntranet:8080
$_SERVER['https_proxy'] => http://proxy.myIntranet:8080
$_SERVER['http_proxy'] => http://proxy.myIntranet:8080

((grep -i "url"))
/etc/php.d/20-curl.ini,
allow_url_fopen => On => On
allow_url_include => Off => Off
curl
cURL support => enabled
cURL Information => 7.76.1
curl.cainfo => no value => no value
url_rewriter.hosts => no value => no value
url_rewriter.tags => form= => form=
my proxy
at .bashrc i имеет

Код: Выделить всё

export http_proxy=http://proxy.MyDomain.br:8080
export https_proxy=http://proxy.MyDomain.br:8080
и curl не нуждаются в curlopt_proxyauth и т. Д. Как показано выше, работает без дополнительных заголовков. Другая проблема, все примеры здесь, в Руководстве PHP, находятся в форме «Прокси» => 'tcp: //etc.etc' Но мой прокси не использует tcp: (!).

Код: Выделить всё

$opts = [
'http' => [
'proxy' => 'http://proxy.redecorp.br:8080',
'request_fulluri'=>true,
'user_agent' => 'Mozilla/5.0'
//,'header' => ["Proxy-Authorization: Basic "]
],
'https' => [ // copy of http
'proxy' => 'http://proxy.redecorp.br:8080',
'request_fulluri'=>true,
'user_agent' => 'Mozilla/5.0'
//,'header' => ["Proxy-Authorization: Basic "]
]
];
$context = stream_context_create($opts);
echo file_get_contents($url,false,$context);
Новая ошибка: "Php Warning: file_get_contents (https://getcomposer.org/versions): не удалось открыть поток: невозможно найти транспорт сокета" http " - вы забыли включить его, когда"

Подробнее здесь: https://stackoverflow.com/questions/797 ... -to-enable
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»