PHP check if url exists & get response

in this post i will share with you how to PHP check if url exists & get response from the server. also learn how to perform a POST request using Curl . lets start this turorial and check http status code php curl + source code.

Check http status code curl php code example :


THE COMPLET CURL PHP CODE SOURCE :

<style>
.mycss{
color: green;
border:1px solid #000;
background: #cff;
font-size: 20px;

}

.box3{

border-color: #EDFF2F;
color: #ADFF2F;
border-width: 0 0 1px 0;
border-style: bold;
font-size:12px;
}
.myurl{
color: red;
border:1px solid #fd0;
background: #ddd;
font-size: 30px;
}

.mycode{
color: darkblue;
border:1px solid #ed0;
background: #ded;
font-size: 25px;
}

</style>

<form method=”post”>
<input type=”url” name=”pageurl” placeholder=”Please enter url” reuired=”reuired” />
<input type=”submit” name=”submit” value=”SUBMIT” />
</form>

<?php
echo “<br>”;

if (isset($_POST[‘pageurl’]))
{
$pageurl = $_REQUEST[‘pageurl’];
$url = $pageurl;
$curl = curl_init($url);

// Use curl_setopt() to set an option for cURL transfer
curl_setopt($curl, CURLOPT_NOBODY, true);

// Use curl_exec() to perform cURL session
$result = curl_exec($curl);

if ($result !== false) {

// Use curl_getinfo() to get information
// regarding a specific transfer
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($statusCode == 404) {
echo “<p class=’box3′>$url<p/>”;
echo “<p class=’mycss’>THIS URL Doesn’t Exist<p/>” . “<br/>”;
echo “<br>”;

echo “<p class=’box3>The StatusCode is:”.”<p class=’mycode’>$statusCode<p/>”;

}
else {

echo “<p class=’myurl’>$url<p/>” . “<p class=’mycss’>THIS URL Exist<p/>” . “<br/>”;

echo “<p class=’myurl’>The StatusCode is:”.”<p class=’mycode’>$statusCode<p/>”;
}
}
else {
echo “<br>”;

echo “<p class=’myurl’>$url<p/>”;
echo “<p class=’mycss’>This URL Doesn’t Exist<p/>” . “<br/>”;
echo “<br>”;

echo “<p class=’myurl’>The StatusCode is:”.”<p class=’mycode’>$statusCode<p/>”;

$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $url);
curl_setopt($curlHandle, CURLOPT_HEADER, true);
curl_setopt($curlHandle, CURLOPT_NOBODY , true); // we don’t need body
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_exec($curlHandle);
$response = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle); // Don’t forget to close the connection

echo $response,””;

}
}

?>

The Form :

<form method=”post”>
<input type=”url” name=”pageurl” placeholder=”Please enter url” reuired=”reuired” />
<input type=”submit” name=”submit” value=”Check HTTP Status” />
</form>

Curl Command To Check HTTP Status Code Curl :

CURLINFO_EFFECTIVE_URL: Last actual URL

CURLINFO_HTTP_CODE: The last response code. As of cURL 7.10.8 this is a legacy alias of CURLINFO_RESPONSE_CODE

CURLINFO_FILETIME: Remote date of retrieved document, with CURLOPT_FILETIME enabled; if -1 is returned the date of the remote document is unknown.

CURLINFO_TOTAL_TIME: Transaction duration in seconds for the last transfer

CURLINFO_NAMELOOKUP_TIME: Domain name resolution time in seconds

CURLINFO_CONNECT_TIME: Connection establishment time in seconds

CURLINFO_PRETRANSFER_TIME: Time in seconds between start of transaction and start of file transfer

CURLINFO_STARTTRANSFER_TIME: Time in seconds until the first byte is about to be transferred

CURLINFO_REDIRECT_COUNT: Number of redirects, with the CURLOPT_FOLLOWLOCATION option enabled

CURLINFO_REDIRECT_TIME: Duration in seconds of all redirect steps before the final transaction is started, with the CURLOPT_FOLLOWLOCATION option enabled

CURLINFO_REDIRECT_URL – With CURLOPT_FOLLOWLOCATION option disabled: Redirect URL found in last transaction, which will need to be manually queried afterwards. If the

CURLOPT_FOLLOWLOCATION option is activated: this value is empty. In this case, the redirect url is available in CURLINFO_EFFECTIVE_URL

CURLINFO_PRIMARY_IP – IP address of the most recent connection

CURLINFO_PRIMARY_PORT – Destination port of most recent connection

CURLINFO_LOCAL_IP – Local (source) IP address of most recent connection

CURLINFO_LOCAL_PORT – Local (source) port of most recent connection

CURLINFO_SIZE_UPLOAD: Total number of bytes uploaded

CURLINFO_SIZE_DOWNLOAD: Total number of bytes downloaded

CURLINFO_SPEED_DOWNLOAD: Average download speed

CURLINFO_SPEED_UPLOAD: Average upload speed

CURLINFO_HEADER_SIZE: Size of received headers

CURLINFO_HEADER_OUT: The query string sent. For this to work, call curl_setopt() with the CURLINFO_HEADER_OUT option.

CURLINFO_REQUEST_SIZE: Total size of requests sent. Currently only for HTTP requests

CURLINFO_SSL_VERIFYRESULT: SSL certification verification result requested by CURLOPT_SSL_VERIFYPEER

CURLINFO_CONTENT_LENGTH_DOWNLOAD: Size of download body, read from Content-Length header:

CURLINFO_CONTENT_LENGTH_UPLOAD: Specified upload size.

CURLINFO_CONTENT_TYPE: Content-Type: of the requested document. null indicates that the server did not send a Content-Type header:

CURLINFO_PRIVATE – Private data associated with the cURL handler, previously set with the CURLOPT_PRIVATE option of the curl_setopt() function

CURLINFO_RESPONSE_CODE – The latest response code

CURLINFO_HTTP_CONNECTCODE – The CONNECT response code

CURLINFO_HTTPAUTH_AVAIL – Bit mask indicating the authentication method(s) available according to the previous answer

CURLINFO_PROXYAUTH_AVAIL – Bitmask indicating available proxy authentication method(s) according to previous answer

CURLINFO_OS_ERRNO – Errno since login failed. The number is system and OP specific.

CURLINFO_NUM_CONNECTS – Number of connections curl had to create to complete the previous transfer

CURLINFO_SSL_ENGINES – Supported OpenSSL crypto-engine

CURLINFO_COOKIELIST – All known cookies

CURLINFO_FTP_ENTRY_PATH – FTP server entry path

CURLINFO_APPCONNECT_TIME – Time in seconds it took from the start until the SSL/SSH connection/handshake to the remote host was completed.

CURLINFO_CERTINFO – TLS certificate chain

CURLINFO_CONDITION_UNMET – Info on unmet conditional time

CURLINFO_RTSP_CLIENT_CSEQ – Next RTSP CSeq client

CURLINFO_RTSP_CSEQ_RECV – Recently Received CSeq

CURLINFO_RTSP_SERVER_CSEQ – Next RTSP CSeq server

CURLINFO_RTSP_SESSION_ID – RTSP session ID

CURLINFO_CONTENT_LENGTH_DOWNLOAD_T – The content-length of the download. This value is read from the Content-Type: field. -1 if size is unknown

CURLINFO_CONTENT_LENGTH_UPLOAD_T – The size specified for the upload. -1 if size is unknown

CURLINFO_HTTP_VERSION – The version used during the last HTTP connection. The return value will be one of the defined CURL_HTTP_VERSION_* constants, or 0 if the version cannot be determined

CURLINFO_PROTOCOL – The protocol used during the last HTTP connection. The returned value will be exactly one of the CURLPROTO_* values

CURLINFO_PROXY_SSL_VERIFYRESULT – The certificate verification result that was requested (using the CURLOPT_PROXY_SSL_VERIFYPEER option). Used only for HTTPS proxies

CURLINFO_SCHEME – The URL scheme used for the most recent login

CURLINFO_SIZE_DOWNLOAD_T – Total number of bytes that have been downloaded. The number is only for the last transfer and will be reset again for each new t

Download Curl Php code source Zip