#php #curl #server #wamp
#php #curl #сервер #wamp
Вопрос:
я пытаюсь подключиться с помощью curl на сервере, на котором установлен wamp, но я получаю отказ в подключении Failed to connect to centrala.ratt.ro port 47654: Connection refused
, и я не знаю почему. Если я получаю доступ к ссылке в браузере, это работает, с php curl это не работает. Вот мой код:
$url = "http://centrala.ratt.ro:47654/webhook/coordonate.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_PORT, 47654);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode ($result));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, 0);
// This should be the default Content-type for POST requests
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
$rezult = curl_exec($ch);
echo $rezult;
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
curl_close($ch);
Ответ №1:
Посмотрел на https://gustavostraube.wordpress.com/2016/08/31/debugging-requests-with-curl /;
/*
* We're going to use the output buffer to store the debug info.
*/
ob_start();
$out = fopen('php://output', 'w');
$handler = curl_init($url);
/*
* Here we set the library verbosity and redirect the error output to the
* output buffer.
*/
curl_setopt($handler, CURLOPT_VERBOSE, true);
curl_setopt($handler, CURLOPT_STDERR, $out);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($handler);
fclose($out);
/*
* Joining debug info and response body.
*/
$data = ob_get_clean();
$data .= PHP_EOL . $response . PHP_EOL;
echo $data;
Просмотрите / опубликуйте вывод для $ out, чтобы определить следующие шаги; Если вы можете, вам следует попытаться запустить curl в командной строке с параметрами ‘-vvv’, чтобы получить аналогичный вывод.
Комментарии:
1.
curl -vvv POST http://centrala.ratt.ro:47654 * getaddrinfo(3) failed for POST:80 * Couldn't resolve host 'POST' * Closing connection #0 curl: (6) Couldn't resolve host 'POST' * About to connect() to centrala.ratt.ro port 47654 (#0) * Trying 86.125.113.237... Connection refused * couldn't connect to host * Closing connection #0 curl: (7) couldn't connect to host
2. Попробуйте:
curl -vvv -X POST -H "Content-Type: application/json" -d "{ "key1": "value1" }" http://centrala.ratt.ro:47654/webhook/coordonate.php
(в Windows); Прямо сейчас я вижу, что он возвращает200
код результата и не возвращает дополнительный контент3. из команды в Windows это работает. Чем не так с моим скриптом?
4. Если это работает в Windows, и вы все еще получаете
Connection refused
из PHP, попробуйте добавить $ out, как описано выше. Это должно дать некоторое представление о том, что происходит