PHP - Как проверить, действительно ли Curl отправляет / отправляет запрос? - PullRequest
2 голосов
/ 06 октября 2010

Я в основном создал скрипт, использующий Curl и PHP, который отправляет данные на сайт, например. хост, порт и время. Затем он представляет данные. Как я узнаю, что Curl / PHP действительно отправляет эти данные на веб-страницы?

$fullcurl = "?host=".$host."&time=".$time.";

Есть ли какие-нибудь способы проверить, действительно ли они отправили данные на эти URL-адреса в My MYSQL?

Ответы [ 2 ]

16 голосов
/ 04 декабря 2010

Вы можете использовать curl_getinfo(), чтобы получить код состояния ответа следующим образом:

// set up curl to point to your requested URL
$ch = curl_init($fullcurl);
// tell curl to return the result content instead of outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// execute the request, I'm assuming you don't care about the result content
curl_exec($ch);

if (curl_errno($ch)) {
    // this would be your first hint that something went wrong
    die('Couldn\'t send request: ' . curl_error($ch));
} else {
    // check the HTTP status code of the request
    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($resultStatus == 200) {
        // everything went better than expected
    } else {
        // the request did not complete as expected. common errors are 4xx
        // (not found, bad request, etc.) and 5xx (usually concerning
        // errors/exceptions in the remote script execution)

        die('Request failed: HTTP status code: ' . $resultStatus);
    }
}

curl_close($ch);

Для справки: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

Или, если вы отправляете запросы к какому-либо API, который возвращает информацию о результате запроса, вам потребуется получить этот результат и проанализировать его. Это очень специфично для API, но вот пример:

// set up curl to point to your requested URL
$ch = curl_init($fullcurl);
// tell curl to return the result content instead of outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

// execute the request, but this time we care about the result
$result = curl_exec($ch);

if (curl_errno($ch)) {
    // this would be your first hint that something went wrong
    die('Couldn\'t send request: ' . curl_error($ch));
} else {
    // check the HTTP status code of the request
    $resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($resultStatus != 200) {
        die('Request failed: HTTP status code: ' . $resultStatus);
    }
}

curl_close($ch);

// let's pretend this is the behaviour of the target server
if ($result == 'ok') {
    // everything went better than expected
} else {
    die('Request failed: Error: ' . $result);
}
0 голосов
/ 06 октября 2010

, чтобы быть уверенным, что curl отправляет что-то, вам понадобится анализатор пакетов. Например, вы можете попробовать wireshark .

Надеюсь, это вам поможет,

Джером Вагнер

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...