首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -如何检查Curl是否真的是post/send请求?

问PHP -如何检查Curl是否真的是post/send请求?
EN

Stack Overflow用户
提问于 2010-10-06 06:17:26
回答 2查看 24.1K关注 0票数 7

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

代码语言:javascript
复制
$fullcurl = "?host=".$host."&time=".$time.";

有没有办法查看他们是否真的将数据发送到我MYSQL上的那些URL?

EN

回答 2

Stack Overflow用户

发布于 2010-12-05 03:46:27

您可以使用curl_getinfo()来获取响应的状态码,如下所示:

代码语言:javascript
复制
// 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的,但这里有一个示例:

代码语言:javascript
复制
// 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);
}
票数 25
EN

Stack Overflow用户

发布于 2010-10-06 06:23:51

为了确保curl发送某些内容,您需要一个数据包嗅探器。例如,您可以尝试使用wireshark。

我希望这能帮到你

杰罗姆·瓦格纳

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3868183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档