繁体   English   中英

PHP - 如何检查Curl是否实际发布/发送请求?

[英]PHP - How to check if Curl actually post/send request?

我基本上使用Curl和PHP创建了一个脚本,它将数据发送到网站,例如主机,端口和时间。 然后它提交数据。 我如何知道Curl / PHP是否实际将这些数据发送到网页?

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

有什么方法可以看看他们是否真的将数据发送到My MYSQL上的那些URL?

您可以使用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);
}

为了确保curl发送一些东西,你需要一个数据包嗅探器。 你可以尝试使用wireshark

我希望这能帮到您,

杰罗姆瓦格纳

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM