Php 如何检查变量中是否存在此代码
我正在查看Google在PHP中的一个CURL结果,它有时会导致使用以下格式的重定向:Php 如何检查变量中是否存在此代码,php,curl,Php,Curl,我正在查看Google在PHP中的一个CURL结果,它有时会导致使用以下格式的重定向: Please click <a href="https://www.google.com/search?q=site:test.com//&gbv=1&sei=vGn6UobNMaqssQTo4IHoDA">here</a> if you are not redirected 如果未重定向,请单击 如何检查变量$result中是否有此项,然后使用PHP获取链接hr
Please click <a href="https://www.google.com/search?q=site:test.com//&gbv=1&sei=vGn6UobNMaqssQTo4IHoDA">here</a> if you are not redirected
如果未重定向,请单击
如何检查变量$result中是否有此项,然后使用PHP获取链接href中的URL?if(isset($result)){
if(isset($result )){
$url = preg_replace('/.*Please click <a href="([^"]*)".*/', "$1", $result );
print $url;
}
$url=preg_replace('/.*请单击使用DomDocument
解析HTML并提取url。请注意,如果您使用的脚本与您请求数据的服务器的ToS相反,则可能会产生法律和财务后果
我正在使用strpos
快速检查提示,在提交资源之前需要解析页面
if (strpos($result, 'if you are not redirected') !== false) {
$doc = new DOMDocument();
@$doc->loadHTML($result);
$newUrl = $doc->getElementsByTagName('a')->item(0)->getAttribute('href');
// new request using $newUrl
}
文档
strpos
-
DomDocument
-
doElement
-
doElement::getAttribute
-
我怀疑您是否被允许这样做,但无论如何都要使用DOMDocument
解析内容。if($var1==$var2){//do something}
这是谷歌的速率限制,最终还将包括一个验证码。你需要从原始结果URL设置引用器标题。这将很快比你想象的更复杂。他们这样做的确切目的是阻止像你这样的脚本。它不是解析整个html页面。只有几行。
if (strpos($result, 'if you are not redirected') !== false) {
$doc = new DOMDocument();
@$doc->loadHTML($result);
$newUrl = $doc->getElementsByTagName('a')->item(0)->getAttribute('href');
// new request using $newUrl
}