[英]Check cURL output PHP
i'm trying to check the output of a cURL. 我正在尝试检查cURL的输出。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$curlresult=curl_exec ($ch);
curl_close ($ch);
if ($curlresult == "OK") {
$result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
} else {
$result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
}
echo $result;
?>
The URL (https://blablabla.com) is a URL that just displays OK. URL(https://blablabla.com)是一个只显示OK的URL。 So, using the code, I would expect to see
所以,使用代码,我希望看到
"The curl action was succeeded! (OUTPUT of curl is: OK)"
“卷曲动作成功了!(卷曲的输出是:好的)”
But, what I do get is: 但是,我得到的是:
The curl action has FAILED!
卷曲动作失败了! (OUTPUT of curl is: OK )
(卷曲的输出是:OK)
I guess i'm making some stupid mistake. 我想我犯了一些愚蠢的错误。 How can I check if https://blablabla.com contains "OK"?
如何检查https://blablabla.com是否包含“OK”?
Thanks! 谢谢!
You may be getting extra white spaces before or after the OK or other characters. 在OK或其他字符之前或之后,您可能会获得额外的空格。
I suggest doing what the people above suggested by testing what is exactly inside the array with var_dump($curlresult);
我建议通过使用
var_dump($curlresult);
测试数组内部的内容来完成上面提到的操作var_dump($curlresult);
or print_r($curlresult);
或
print_r($curlresult);
But alternatively you could instead of matching that $curlresult equals only "OK", you could test if $curlresult contains "OK" inside of it. 但另外你可以代替匹配$ curlresult仅等于“OK”,你可以测试$ curlresult是否包含“OK”。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$curlresult=curl_exec ($ch);
curl_close ($ch);
if (preg_match("/OK/i", $curlresult)) {
$result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
} else {
$result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
}
echo $result;
?>
you can do 你可以做
$info = curl_getinfo($ch);
var_dump($info);
gives you info on http status code returned and connect time etc. 为您提供返回的http状态代码和连接时间等信息。
You should try var_dump($curlresult);
你应该尝试
var_dump($curlresult);
to see what you're really getting, otherwise i think there may be a problem with using SSL (HTTPS), to quick fix this (accept any server certificate): 看看你真正得到了什么,否则我认为使用SSL(HTTPS)可能存在问题,快速修复此问题(接受任何服务器证书):
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://blablabla.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$curlresult=curl_exec ($ch);
curl_close ($ch);
if ($curlresult == "OK") {
$result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
} else {
$result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
}
//var_dump($curlresult);
echo $result;
?>
See, In this code chunk: 请参阅此代码块:
if ($curlresult == "OK") {
$result = "The curl action was succeeded! (OUTPUT of curl is: ".$curlresult.")";
} else {
$result = "The curl action has FAILED! (OUTPUT of curl is: ".$curlresult.")";
}
it is assumed that $curlresult
is string, First try var_dump($curlresult)
to know what datatype response is returned from this call. 假设
$curlresult
是字符串,首先尝试var_dump($curlresult)
以了解从此调用返回的数据类型响应。 May be return type is not string. 可能是返回类型不是字符串。
Then use the check for that datatype in if condition. 然后在if条件中使用该数据类型的检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.