{"id":113,"date":"2013-04-01T18:52:16","date_gmt":"2013-04-01T18:52:16","guid":{"rendered":"http:\/\/ecodebook.wordpress.com\/?p=113"},"modified":"2013-04-01T18:52:16","modified_gmt":"2013-04-01T18:52:16","slug":"php-check-broken-link-using-curl","status":"publish","type":"post","link":"https:\/\/blog.mycarts.shop\/?p=113","title":{"rendered":"PHP: Check broken links using curl"},"content":{"rendered":"<p>[code language=&#8221;php&#8221;]<\/p>\n<p>function check_url($url)<br \/>\n{<br \/>\n\t$ch = curl_init();<br \/>\n\tcurl_setopt($ch, CURLOPT_URL, $url);<br \/>\n\tcurl_setopt($ch, CURLOPT_HEADER, 1);<br \/>\n\tcurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br \/>\n\t$data = curl_exec($ch);<br \/>\n\t$headers = curl_getinfo($ch);<br \/>\n\tcurl_close($ch);<br \/>\n\treturn $headers[&#8216;http_code&#8217;];<br \/>\n}<br \/>\n$mainurl = &quot;http:\/\/www.google.com&quot;;<br \/>\n$myurl = &quot;http:\/\/www.google.com\/gggg&quot;;<\/p>\n<p>$mainsite = check_url($mainurl);<br \/>\n$satus = check_url($myurl);<\/p>\n<p>if($mainsite == &#8216;200&#8217;){<br \/>\n\tif($satus == &#8216;200&#8217;){<br \/>\n\t\techo &quot;Its works&quot;;<br \/>\n\t}else{<br \/>\n\t\techo &quot;broken url&quot;;<br \/>\n\t}<br \/>\n}else{<br \/>\n\techo &quot;&quot;;<br \/>\n}<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[code language=&#8221;php&#8221;] function check_url($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($ch); $headers = curl_getinfo($ch); curl_close($ch); return $headers[&#8216;http_code&#8217;]; } $mainurl = &quot;http:\/\/www.google.com&quot;; $myurl = &quot;http:\/\/www.google.com\/gggg&quot;; $mainsite = check_url($mainurl); $satus = check_url($myurl); if($mainsite == &#8216;200&#8217;){ if($satus == &#8216;200&#8217;){ echo &quot;Its works&quot;; }else{ echo &quot;broken url&quot;; } }else{ echo&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=113"}],"version-history":[{"count":0,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}