php - 带有 httpcode 响应的 PHP CURL 条件
问题描述
我正在尝试在我的 PHP CURL 代码上添加一个条件,以继续执行 curl 直到我得到 httpcode = 200,因为该网站的流量很高,所以当网关响应错误时,如果获取 httpcode 200 做一些说明。
所以我正在尝试使用我的代码,但它没有帮助。
<?php
$ch = curl_init("https://example.com/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_PROXY, '151.80.143.155:53281');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\AppServ\www\cloud\cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.53 Safari/537.36');
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
while(true){
if($http !== 200){
curl_exec($ch);
curl_close($ch);
sleep(8);
}
else{
// Do some instruction ...
sleep(8);
}
}
curl_close($ch);
?>
请问各位大神,这种情况怎么办?
谢谢你们 。
解决方案
我认为您只是错误地命名了一个变量。你正在检查$http
,而你应该检查$httpcode
<?php
$ch = curl_init("https://example.com/index.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_PROXY, '151.80.143.155:53281');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'C:\AppServ\www\cloud\cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.53 Safari/537.36');
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
while(true){
if($httpcode !== 200){ // This line was wrong
curl_exec($ch);
curl_close($ch);
sleep(8);
} else{
// Do some instruction ...
sleep(8);
break;
}
}
curl_close($ch);
?>
推荐阅读
- node.js - 在 node.js 中使用 64 位时间戳
- android - android ConstraintLayout 高度百分比 0.99
- java - 在 Netbeans 中格式化
- java - 解决由于外部传递依赖导致的版本冲突
- php - 带有特殊字符 PHP 的 cURL 文件名
- windows - Windows 回调函数因任何新的回调事件而冻结
- sql-server - 将 NULL 参数与可能包含 NULL 值的列进行比较
- amcharts - IcCube,如何更改 AMChart 小部件中的图例标签?
- symfony4 - Symfony Monolog 仅在出现异常时添加额外的日志记录数据
- ios - 将 xib 文件作为 UITableViewController 中的标头添加到 UITableView