PHP - 如何检查Curl实际发布/发送请求?
问题描述:
我基本上使用Curl和PHP创建了一个脚本,该脚本将数据发送到网站,例如主机,端口和时间。然后它提交数据。我怎么知道Curl/PHP是否真的把这些数据发送到网页上? “?主机=”。PHP - 如何检查Curl实际发布/发送请求?
$ fullcurl = $主机 “&时间=” $时间“;?
,看看他们是否实际发送数据到我的MySQL这些网址的任何方式
答
您可以使用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);
}
不,我听说我需要正则表达式或curlopt。 – Ray 2010-10-05 22:36:56
你好。我想我误解你的问题。 curl工作和mysql之间有什么关系? – 2010-10-06 06:56:41
基本上,curl从MYSQL抓取URL,然后发送发布数据给他们。 – Ray 2010-10-06 07:21:12