Fast check files exists in other server - php

I try check is exists in other file, if file exists server response HTTP 200 if not HTTP 302.
now i try check with get_headers() function but it to slow, because files size about 2mb-10mb and i check about 20 file in a while and it spend about 5-10 sec.
Perhaps there are other options?

Try using curl with the CURLOPT_NOBODY option.
Example:
$ch = curl_init("http://www.other-server.com/file.jpg");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$response_code will contain the HTTP response code.

Related

How To Bypass Cloudfare using php or javascript

My problem is
I have made curl request on paxfull api earlier it was returning result but now its returning 503 .
Here is my code
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, 'https://paxful.com/buy-bitcoin?format=json');
curl_setopt($handle, CURLOPT_POST, false);
curl_setopt($handle, CURLOPT_BINARYTRANSFER, false);
curl_setopt($handle, CURLOPT_HEADER, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 10);
$response = curl_exec($handle);
$hlength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$body = substr($response, $hlength);
// If HTTP response is not 200, throw exception
if ($httpCode != 200) {
throw new Exception($httpCode);
}
I get this error:
Error Fatal error: Uncaught exception 'Exception' with message '503'
I googled and found it might be ip address blocked but when made get request at browser its giving results to me.
now i came with conclusion they are not allowing any GET Request .
if you run url https://paxful.com/buy-bitcoin?format=json it first check browser the return the result.
how can we get results of paxfull api. please suggest
here is snapshot
then there api redirects 404
url http://localhost/cdn-cgi/l/chk_jschl?jschl_vc=c5b74eae14eb1b1e5862f913b9f0f178&pass=1499953121.017-h%2FljgkjMr%2B&jschl_answer=18913
Its not possible through javascript also
fiddle http://jsfiddle.net/00cvyyuo/350/
i found link How to bypass cloudflare bot/ddos protection in Scrapy?
but this link helps in python so can someone help in php or javascript.
try to use Chrome's network tab in Developer Tools(f12) to see the actual request being sent. If it works then try to repeat the request from a REST client, where you can edit the headers to check what works and what not.
If you got it working then set all the headers in cURL. If it fails then set verbose and check what was sent.

Check if an image exist using HTTPS

I am just wondering if there is a way to check if an encrypted image exist over https.
I am using this code below to check whether an image exist. It will return 200 if it does, and 404 if it doesn't. But this only work on image URL that does not contain https.
function exists_url($uri){
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
return $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
If I try to verify this url ( https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTup5KSMveqkgrDKZR6p-0ANhPkJ7srbJOlKR78DUqqh85I_3MUrw ) it gives me a result of 0 instead of 200 or 404.
I have thought to use getimagesize() function to accomplish this task. But even this function only works for image that is not send over HTTPS.
Since you're using SSL/TLS you must point cURL to a valid CA Certificate bundle. The bundle must be in .pem format. You can obtain such a bundle from http://curl.haxx.se/ca/.
Next you need to tell cURL to use your CA bundle. You can do this with the CURLOPT_CAINFO option.
curl_setopt($ch, CURLOPT_CAINFO, '/path_to_file/cacert.pem');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
You could simply deactivate the verify of peer with
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
So you have not to worry about nothing.
Watch out: #user555 answer is better for security purposes
A response code of 0 from Curl typically means there was an error connecting to the server or something along those lines (See PHP cURL HTTP CODE return 0 and countless other questions).
I did however run this code myself using PHP 5.5.x and it returned a 200 as expected. getimagesize() not working also leads me to think it's most likely a problem with curl or php on your system, what versions of them do you have?

Wordpress API- XML-RPC: wp.newPost returns false roughly 40% of the time

The API documentation clearly states that the wp.newPost returns the new post id, but roughly 40% of the time we're getting a response of false, but with and HTTP Response Code of 100. The post is published to Wordpress successfully either way.
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $this->getXMLRPCURL());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
// Encode htaccess credentials if they're set
$htaccess_credentials = $this->getHtaccessCredentials();
if ($htaccess_credentials) {
curl_setopt($ch, CURLOPT_USERPWD, $htaccess_credentials->toString());
}
$results = curl_exec($ch); // <<-- RETURNS FALSE HERE.
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Returns 100
curl_close($ch);
return $results;
I'm happy to post a sample $request string, but that part seems to be working fine. I was just wondering if anyone has run into this before? It seems to have been run into here, but with little resolution.
Sheesh. Timeout. Duh. Someone had set the CURL timeout to 1. Nonetheless, we're still not receiving full responses sometimes even without a timeout set - likely the default socket timeout is kicking in. Nonetheless, we have a (somewhat weighty) workaround.
Have a nice day.

Handle 403 error without making redirection

I wanted to handle 403 error without using server side error redirection methods(i.e. without using .htaccess file). Sometimes server returns a 403 error message (forbidden access). So is it possible to have PHP script which handles this 403 error message?
For example, Before showing error page, I would like to obtain server status when it my specific php page runs, and without making a redirection, I would just like to display custom message in the that page.
Some solutions for you.
Check for URL errors and make sure the actual web page is specified. Its common reason for a web site to return the 403 Forbidden error, when the URL is pointing to a directory instead of a web page. Which can be done using HttpRequest Class in PHP. You can use http_get to perform GET request. You can also Test URL here.
<?php
$response = http_get("URL", array("timeout"=>1), $info);
print_r($info);
?>
Output:
array (
'effective_url' => 'URL',
'response_code' => 403,
.
and so on
)
What is important for you is response_code with which you can play further.
Use of curl.
function http_response($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if(!$head)
{
return FALSE;
}
return $httpCode;
}
$errorcode = http_response("URL"); //if success 200 otherwise different

file_get_contents throws 400 Bad Request error PHP

I'm just using a file_get_contents() to get the latest tweets from a user like this:
$tweet = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline/User.json'));
This works fine on my localhost but when I upload it to my server it throws this error:
Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline/User.json) [function.file-get-contents]:failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request...
Not sure what might be causing it, maybe a php configuration I need to set on my server?
Thanks in advance!
You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);
This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.
You can use file_get_contents adding the ignore_errors option set to true, in this way you will get the entire body of the response in case of error (HTTP/1.1 400, for example) and not only a simple false.
You can see an example here: https://stackoverflow.com/a/11479968/3926617
If you want access to response's headers, you can use $http_response_header after the request.
http://php.net/manual/en/reserved.variables.httpresponseheader.php
Just a little addendum on Ben's answer.
According to the PHP manual the CURLOPT_URL option may be set when inizializing the cURL handle with curl_init().
// make request
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);

Categories