PHP cheapest way how to check if remote image exist - php

I want to check if a remote image exist.
Currently using CURL and based on its response to i can determine if the file exist.
However, this takes a fare bit amount of time depending on the image file size also.
Is there a safest and cheapest way of doing a check without doing it offline or using cache?
$ch = curl_init();
// set URL and other appropriate options
/*--Note: if in sabre proxy, please activate the last 4 lines--*/
$options = array( CURLOPT_URL => $img,
CURLOPT_HEADER => false, // get the header
CURLOPT_NOBODY => true, // body not required
CURLOPT_RETURNTRANSFER => false, // get response as a string from curl_exec, not echo it
CURLOPT_FRESH_CONNECT => false, // don't use a cached version of the url
CURLOPT_FAILONERROR => true,
CURLOPT_TIMEOUT => 5
);
curl_setopt_array($ch, $options);
if(!curl_exec($ch)) { return FALSE; }
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode<400);

Perform a HEAD request and check the response code.

Related

Get HTTP response body content from site for parsing with PHP

I know there are lots of articles on how to get response with Curl and then decode it but non of them worked. Sending request with CURL. Getting response. Dumping response content on the screen shows only "String(996)". I guess its compressed string. Tried to decompress it but didnt work.
Here is my case if someone can help me with that.
I tried this code with different url and it worked.
<?php
//set up variables
$url = 'https://en.bidfax.info';
$response = get_web_page($url);
var_dump ($response);
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Went through all posts about curl and gzdecode / encode.

Square API Won't Return List of Locations - Invalid Request Error

Here is my code:
// Build request URL
$url = 'https://connect.squareup.com/v2/locations/';
// Build and execute CURL request
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
var_dump($content);
Here is what I get back:
string(158) "{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"NOT_FOUND","detail":"API endpoint for URL path `/v2/locations/` and HTTP method `GET` is not found."}]}"
I am pounding my head on this one... I tried using the Square SDK but calling from it doesn't return a list of locations either.
I have created an application on the Square Developer Dashboard. $accessToken is set to the sandbox access token listed there.
You have added an extra / at the end of the url. You should instead use:
$url = 'https://connect.squareup.com/v2/locations';
Other than that your code works!

How to flush requests faster to client using curl for apns on http/2

Hello this php script using curl takes close to 10 min to complete the sending of 2500 push notifications on Apple HTTP/2 APNS server.
How can I accelerate the number of http request sent to the same server if the url changes every time.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0,
CURLOPT_URL => $url,
CURLOPT_PORT => 443,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $json_alert,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HEADER => 1
));
// send push for each token
foreach ($all_tokens as $device_token) {
$url = "{$base_url}/3/device/{$device_token}";
// set url per device token
curl_setopt($curl, CURLOPT_URL, $url);
// go...
$result = curl_exec($curl);
// get response
$status = curl_getinfo($curl);
if ($status['http_code'] == '410') {
// device does not accept push for this app anymore
$bad_tokens[] = $device_token;
} else if($status['http_code'] == '200') {
$push_sent_succss_count += 1;
}
}
curl_close($curl);
You could use PHP's cURL multi handle, or use a library, such as Guzzle, with batching functions.
These will allow you to send the requests in parallel and either handle the responses as they arrive or wait for the last/ slowest one and handle all the responses then.

PHP cURL response error when comparing in if statement

Ok, this literally makes no sense...
I am using this function to call an API
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "API", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Which works find if i do something like this
$exampleResponse = get_web_page("https://example.com/index.php?example=1");
echo $exampleResponse;
But what makes absolutely no sense (to me) is that trying to compare the response in a if statement DOES NOT WORK! Here is an example (assume the API response is "example")
$exampleResponse = get_web_page("https://example.com/index.php?example=1");
if($exampleResponse == "example")
{
echo "ok";
}
This will not work and ok will NOT be echoed even though the API responded with "example". This is very confusing to me, if anyone has any idea why this may be occurring please let me know - thanks for your time.

PHP: Curl not called URL and got Error No =0

I am developing one SMS application. I have to call one link
for eg http://sendsms.com/send.php?mobile=45455&msg=hello for sending SMS
so I used CURL concept to send sms.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
CURLOPT_TIMEOUT => 15, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$output ="Error:".$err." ErrMsg:".$errmsg." Header:". json_encode($header);
The Problem is message is not sending. I checked the SMS API server. And There also no request is received. So the only problem is CURL may not calling the server. I tried to print the Error msg and I got curl_errno($ch) is 0
please provide me the best way to do this
Try to send request by POST:
curl_setopt($ch, CURLOPT_POST ,1);

Categories