Запросы Операция не работает на Raw PHP - PullRequest
0 голосов
/ 07 января 2020

Я пытался реализовать вызов API Speci c из API, который я создал. Это необработанный PHP, рамки не используются. Просто обычный php файл . Поскольку я знаю, что я правильно реализовал код и несколько раз протестировал на сервере MAMP, но проблема в Каждый раз, когда я пытаюсь вызвать API, я получаю 500 Internal Server Error .

Я скачал файл Requests. php из этого скрипта из git: git clone git://github.com/rmccue/Requests.git

Путь к файлу определен правильно, так как я его повторно проверил ,

Возможно, файл запроса не реализуется. Включили файл запроса в мой файл php.

Код:

<?php
    include('Applications/MAMP/htdocs/api/Requests/library/Requests.php');
    $response = array();
    $request_method = $_SERVER['REQUEST_METHOD'];

    if( $request_method == 'GET' ){
        Requests::register_autoloader();
        $response = Requests::get('https://nut-case.s3.amazonaws.com/jobs.json');
        echo $response;
    }else {
        echo 'No defined function for this method. Please use GET only';
    }
?>

PHP Журналы ошибок

Stack trace:
#0 /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php(177): Requests_Transport_cURL->process_response('{"data":[{"_id"...', Array)
#1 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(379): Requests_Transport_cURL->request('https://nut-cas...', Array, NULL, Array)
#2 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(231): Requests::request('https://nut-cas...', Array, NULL, 'GET', Array)
#3 /Applications/MAMP/htdocs/api/index.php(29): Requests::get('https://nut-cas...')
#4 {main}
  thrown in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php on line 422
[07-Jan-2020 13:26:51 UTC] PHP Fatal error:  Uncaught Requests_Exception: cURL error 28: Operation timed out after 10000 milliseconds with 1131134 out of 1459668 bytes received in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php:422
Stack trace:
#0 /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php(177): Requests_Transport_cURL->process_response('{"data":[{"_id"...', Array)
#1 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(379): Requests_Transport_cURL->request('https://nut-cas...', Array, NULL, Array)
#2 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(231): Requests::request('https://nut-cas...', Array, NULL, 'GET', Array)
#3 /Applications/MAMP/htdocs/api/index.php(29): Requests::get('https://nut-cas...')
#4 {main}
  thrown in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php on line 422
[07-Jan-2020 13:27:04 UTC] PHP Fatal error:  Uncaught Requests_Exception: cURL error 28: Operation timed out after 10000 milliseconds with 1113726 out of 1459668 bytes received in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php:422
Stack trace:
#0 /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php(177): Requests_Transport_cURL->process_response('{"data":[{"_id"...', Array)
#1 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(379): Requests_Transport_cURL->request('https://nut-cas...', Array, NULL, Array)
#2 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(231): Requests::request('https://nut-cas...', Array, NULL, 'GET', Array)
#3 /Applications/MAMP/htdocs/api/index.php(29): Requests::get('https://nut-cas...')
#4 {main}
  thrown in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php on line 422
[07-Jan-2020 13:28:02 UTC] PHP Fatal error:  Uncaught Requests_Exception: cURL error 28: Operation timed out after 10000 milliseconds with 1096318 out of 1459668 bytes received in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php:422
Stack trace:
#0 /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php(177): Requests_Transport_cURL->process_response('{"data":[{"_id"...', Array)
#1 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(379): Requests_Transport_cURL->request('https://nut-cas...', Array, NULL, Array)
#2 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(231): Requests::request('https://nut-cas...', Array, NULL, 'GET', Array)
#3 /Applications/MAMP/htdocs/api/index.php(29): Requests::get('https://nut-cas...')
#4 {main}
  thrown in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php on line 422
[07-Jan-2020 13:28:16 UTC] PHP Fatal error:  Uncaught Requests_Exception: cURL error 28: Operation timed out after 10000 milliseconds with 1113726 out of 1459668 bytes received in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php:422
Stack trace:
#0 /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php(177): Requests_Transport_cURL->process_response('{"data":[{"_id"...', Array)
#1 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(379): Requests_Transport_cURL->request('https://nut-cas...', Array, NULL, Array)
#2 /Applications/MAMP/htdocs/api/Requests/library/Requests.php(231): Requests::request('https://nut-cas...', Array, NULL, 'GET', Array)
#3 /Applications/MAMP/htdocs/api/index.php(29): Requests::get('https://nut-cas...')
#4 {main}
  thrown in /Applications/MAMP/htdocs/api/Requests/library/Requests/Transport/cURL.php on line 422

URL-адрес в порядке. Я пытаюсь мой API на моем локальном хосте, используя MAMP, и API работает нормально. Это для целей тестирования, так как я учу PHP впервые. Пожалуйста помоги. Спасибо

1 Ответ

1 голос
/ 07 января 2020

Может быть, вы можете попробовать это, чтобы получить данные.

 if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['phone_num'])) {

  if (trim($_POST['phone_num']) != '') {
    $ch = curl_init('https://nut-case.s3.amazonaws.com/jobs.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); // The maximum number of seconds to allow cURL functions to execute.
    $data = curl_exec($ch);
    $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    // Check if the request has status 200 ok
    if ($httpStatus == 200) {
      var_dump(json_decode($data));
    } else {
      //return an error
    }
  } else {
    // return error invalid phone_num
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...