Check if tweet status exists? - php

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_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
$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.

You probably have to set the Return Transfer flag
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
If the code returns as 30x status you probably have to add the Follow Location flag as well
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

You can use #get_header. It will return an array in which the first item has the response code:
$response = #get_headers($url);
print_r($response[0]);
if($response[0]=='HTTP/1.0 404 Not Found'){
echo 'Not Found';
}else{
echo 'Found';
}

Related

why does my function login Curl always return true? CodeIgniter

i try to login through website using cURL, i want to give validation login error. when i fail to login the page will show alert, if succesful to login the page still show alert. . what's wrong with my code, any solution? thanks
Here's my library code:
public function loginIdws($username ,$password){
$url = 'http://forum.idws.id/login/login';
$data = '_xfToken=&cookie_check=1&login='.$username.'&password='.$password.'&redirect=http://forum.idws.id/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_TIMEOUT,40000);
curl_setopt($ch, CURLOPT_HEADER,FALSE);
curl_setopt($ch, CURLOPT_NOBODY,FALSE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_USERAGENT,"Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_REFERER,$_SERVER['REQUEST_URI']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_POST,TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$baca = curl_exec($ch);
if ($baca=== false){
return false;
}else
{
return true;
}
$info = curl_getinfo($ch);
curl_setopt($ch,CURLOPT_URL,"http://forum.idws.id/account/");
curl_setopt($ch,CURLOPT_POST, 0);
$baca2 = curl_exec($ch);
return false;
curl_close($ch);
$pecah1 = explode('<fieldset>',$baca2);
$pecah2 = explode('</fieldset>',$pecah1[1]);
echo "<fieldset>";
echo $pecah2[0];
echo "</fieldset>";
}
this is my controller code :
public function loginn(){
$username= $this->input->post('login','true');
$password = $this->input->post('password','true');
if($this->ci_curl->loginIdws($username,$password)===false){
echo "<script> alert('Invalid Username & Password');
window.location.href='index';
</script>";
}else {
$this->load->view('curl/thread');
return true;
}
}
codes after never executed,
$baca = curl_exec($ch);
if ($baca=== false){
return false;
}else
{
return true;
}
because you return result anywhere
also curl_exec() can return different result dependent on your configuration
read more Anthony Hatzopoulos answer
I suggest use Requests for web requests in php
First of all check the response, what's it is returning, my guess, it is returning 'true'/'false', so $response==false is not same as say, $response=='false'. So, you either need to check the value or convert it to boolean before doing a strict check, something like:
$baca = curl_exec($ch);
$loginOk = $baca === 'true'? true: false;
return $loginOk;
OR do:
$baca = curl_exec($ch);
if ($baca == 'false'){
return false;
} else {
return true;
}
and it should as expected.

curl to get headers

I want to check if a page gives a 200 header using curl.
I am using the following script:
public static function isUrlExist($url)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_NOBODY, TRUE);
curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($code == 200) {
$status = true;
} else {
$status = false;
}
curl_close($curl);
return $status;
}
The strange thing is it evaluates to true if I query a website such as vimeo: https://vimeo.com/api/oembed.json?url=https://vimeo.com/11896354
But websites such as Facebook or Google return false.
Am I missing something?
My best guess is that facebook/google is redirecting you, causing a 3xx redirect status code. Try adding curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); before curl_exec() to make curl follow redirects.

How to check whether an image is present or not at a specific URL using PHP?

Suppose I've one URL which is supposed to represent an image i.e. if I enter the same URL in an address bar and hit it, the image should display in a browser window.
If the URL doesn't have any image present at it it should return false otherwise it should return true.
How should this be done in an efficient and reliable way using PHP ?
I use this little guy:
function remoteFileExists($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (curl_exec($ch)) return true;
else return false;
}
Use like:
if (remoteFileExists('https://www.google.com/images/srpr/logo11w.png')){
echo 'Yay! Photo is there.';
} else {
echo 'Photo no home.';
}
There are two options:
You can use curl, it is explained here : How can one check to see if a remote file exists using PHP?
Use PHP file_exists() : http://php.net/manual/en/function.file-exists.php
Example :
$file = 'http://www.domain.com/somefile.jpg';
$file_headers = #get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
Try this
$ch = curl_init("https://www.google.com/images/srpr/logo11w.png");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($retcode==200)
echo 'File Exist';

Check if links are broken in php

I wonder if there is any good PHP script (libraries) to check if link are broken? I have links to documents in a mysql table and could possibly just check if the link leads to a the document, or if I am redirected to anther url. Any idea? I would prefer to do it in PHP.
Might be related to:
Check link works and if not visually identify it as broken
You can check for broken link using this function:
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['http_code'];
}
You need to have CURL installed for this to work. Now you can check for broken links using:
$check_url_status = check_url($url);
if ($check_url_status == '200')
echo "Link Works";
else
echo "Broken Link";
Also check this link for HTTP status codes : HTTP Status Codes
I think you can also check for 301 and 302 status codes.
Also another method would be to use get_headers function . But this works only if your PHP version is greater than 5 :
function check_url($url) {
$headers = #get_headers( $url);
$headers = (is_array($headers)) ? implode( "\n ", $headers) : $headers;
return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
}
In this case just check the output :
if (check_url($url))
echo "Link Works";
else
echo "Broken Link";
Hope this helps you :).
You can do this in few ways:
First way - curl
function url_exists($url) {
$ch = #curl_init($url);
#curl_setopt($ch, CURLOPT_HEADER, TRUE);
#curl_setopt($ch, CURLOPT_NOBODY, TRUE);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', #curl_exec($ch) , $status);
return ($status[1] == 200);
}
Second way - if you dont have curl installed - get headers
function url_exists($url) {
$h = get_headers($url);
$status = array();
preg_match('/HTTP\/.* ([0-9]+) .*/', $h[0] , $status);
return ($status[1] == 200);
}
Third way - fopen
function url_exists($url){
$open = #fopen($url,'r');
if($handle !== false){
return true;
}else{
return false;
}
}
First & second solutions
As quick workaround check, you can use the global variable $http_response_header with file_get_contents() function.
For example (extracted from PHP documentation):
<?php
function get_contents() {
file_get_contents("http://example.com");
var_dump($http_response_header);
}
get_contents();
var_dump($http_response_header);
Then check the status code in first line for a "HTTP/1.1 200 OK" or other HTTP status codes.
Try this:
$url = '[your_url]';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if ($result === false) {
echo 'broken url';
} else {
$newUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
if ($newUrl !== $url) {
echo 'redirect to: ' . $newUrl;
}
}
curl_close($curl);
if you looking for a solution in PHP Laravel. check this link
use Illuminate\Support\Facades\Http;
$response = Http::get('http://example.com');
$response->body() : string;
$response->json($key = null) : array|mixed;
$response->object() : object;
$response->collect($key = null) : Illuminate\Support\Collection;
$response->status() : int;
$response->ok() : bool;
$response->successful() : bool;
$response->redirect(): bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

Check if a remote page exists using PHP?

In PHP, how can I determine if any remote file (accessed via HTTP) exists?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); //follow up to 10 redirections - avoids loops
$data = curl_exec($ch);
curl_close($ch);
if (!$data) {
echo "Domain could not be found";
}
else {
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if ($code == 200) {
echo "Page Found";
}
elseif ($code == 404) {
echo "Page Not Found";
}
}
Modified version of code from here.
I like curl or fsockopen to solve this problem. Either one can provide header data regarding the status of the file requested. Specifically, you would be looking for a 404 (File Not Found) response. Here is an example I've used with fsockopen:
http://www.php.net/manual/en/function.fsockopen.php#39948
This function will return the response code (the last one in case of redirection), or false in case of a dns or other error. If one argument (the url) is supplied a HEAD request is made. If a second argument is given, a full request is made and the content, if any, of the response is stored by reference in the variable passed as the second argument.
function url_response_code($url, & $contents = null)
{
$context = null;
if (func_num_args() == 1) {
$context = stream_context_create(array('http' => array('method' => 'HEAD')));
}
$contents = #file_get_contents($url, null, $context);
$code = false;
if (isset($http_response_header)) {
foreach ($http_response_header as $header) {
if (strpos($header, 'HTTP/') === 0) {
list(, $code) = explode(' ', $header);
}
}
}
return $code;
}
I recently was looking for the same info. Found some really nice code here: http://php.assistprogramming.com/check-website-status-using-php-and-curl-library.html
function Visit($url){
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL,$url );
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch,CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$page=curl_exec($ch);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 300){
return true;
}
else {
return false;
}
}
if(Visit("http://www.site.com")){
echo "Website OK";
}
else{
echo "Website DOWN";
}
Use Curl, and check if the request went through successfully.
http://w-shadow.com/blog/2007/08/02/how-to-check-if-page-exists-with-curl/
Just a note that these solutions will not work on a site that does not give an appropriate response for a page not found. e.g I just had a problem with testing for a page on a site as it just loads a main site page when it gets a request it cannot handle. So the site will nearly always give a 200 response even for non-existent pages.
Some sites will give a custom error on a standard page and not still not give a 404 header.
Not much you can do in these situations unless you know the expected content of the page and start testing that the expected content exists or test for some expected error text within the page and that is all getting a bit messy...

Categories