I need a way to check if tweet exists. I have link to tweet like https://twitter.com/darknille/status/355651101657280512 . I preferably want a fast way to check (without retrieving body of page, just HEAD request), so I tried something like this

function if_curl_exists($url)
{   

    $resURL = curl_init(); 
    curl_setopt($resURL, CURLOPT_URL, $url); 
    curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1); 
    curl_setopt($resURL, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($resURL, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'curlHeaderCallback'); 
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1); 
    $x = curl_exec ($resURL); 
    //var_dump($x);
    echo $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE); 
    curl_close ($resURL); 
    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { 
        return false;
    }
    else return true;

}   

or like this

function if_curl_exists_1($url) 
{
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_NOBODY, true);//head request
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($curl);

    $ret = false;

    if ($result !== false) {
        //if request was ok, check response code
        echo $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);  

        if ($statusCode == 200) {
            $ret = true;   
        }
    }

    curl_close($curl);
    return $ret;
}

but both those return null with curl_exec(), there is nothing to check for http status code.

The other way is to use twitter api, like GET statuses/show/:id https://dev.twitter.com/docs/api/1.1/get/statuses/show/%3Aid but there is no special return value if tweet doesn't exist, as said here https://dev.twitter.com/discussions/8802

I need advice whats the fastest way to check, I am doing in php.

It works for all sites like google, php.net but not for twitter, like they server is set not to respond on HEAD requests, or some additional http params. I guess that fact that https:// is used doesn't affect things.

Try this:

<?php

$url = 'https://twitter.com/darknille/status/355651101657280512';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);

curl_exec($ch);


if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);

    echo $intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);

    switch($intReturnCode)
    {
        case 200:
        case 302:
        case 304:
            echo "true";
            break;
        default:
            echo "false";
            break;
    }
}

?>

The problem with your first function is the callback.

Maybe it's a connection problem? I don't see any evident issues, unless your server IP is banned or there is a DNS problem. If you have access to a terminal in your server, try to run this:

curl --head https://twitter.com/

It should return something like:

HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-type: text/html; charset=utf-8
date: Sat, 13 Jul 2013 19:34:13 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Sat, 13 Jul 2013 19:34:13 GMT
pragma: no-cache
server: tfe
...

Try also the dig command, to verify if this is a DNS issue, first run:

dig twitter.com

And then:

dig @8.8.8.8 twitter.com

The first will use default DNS settings, the second will query the DNS of Google.

I solved it. Thnak you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.