I need to check if remote file are online and they didn't change. The problem is that those files are big, so I want to read the http header only and then abort the request. The result would be based on response status code and content length field.
How can I do it in PHP?
You can use the get_headers() function.
If you prefer cURL, you can use CURLOPT_NOBODY:
$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);
$headers = curl_exec($ch);
Related
I am trying to reproduce this POST https://www.screencast.com/t/OUE1StfLfyP7 in php but I couldn't archive that it works correctly because after downloading the image is not possible to open it
$url = "https://internal-api-staging-lb.interact.io/v2/contacts/X/documents/fr_FR9.gif";
$data = array('file' => '#'.realpath('https://www.viventura.de/images/icons/flags/fr_FR.gif'));
// Initialize cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/octet-stream", "authToken: XX"));
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
I am doing something wrong someone can give me a hand? Thanks a lot.
The documentation says like this:
curl —X POST -c cookies.txt —d "login=demo&password=demo42" https://www.myadcash.com/console/login_proxy.php
Output will be:
{"token":"6333531373034343433623663646836383165693937383167373264323334663"}
My current code
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
curl_setopt($ch,CURLOPT_URL,"https://www.myadcash.com/console/login_proxy.php");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
No JSON response is showing. Although the text file is saving. Please help me to find right direction. Also if there is any error in the code, please let me know.
-d "login=demo&password=demo42" means data field, thats why pass as post fields not headers.
Therefore these two lines in your curl
curl_setopt($ch, CURLOPT_POST, 1); //Optional
curl_setopt($ch, CURLOPT_POSTFIELDS, "login=demo&password=demo42");
and you must get output.
On the basis of documentation, credentials must be post fields not header that's why no need to put login and password on header.
You don't need
curl_setopt($ch, CURLOPT_HTTPHEADER, array("login"=>"demo","password"=>"demo42"));
I'm trying to grab cookies from a remote host with a little script which works well:
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
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_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIE, TRUE);
// grab URL and pass it to the browser
$opt = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
I would like to know how I can pass them back to the client?
I'm developing a small proxy in PHP with cURL. It has to receive http requests from a client, make some statistics with this requests, foward the request to the web server and foward the responses to the client. Everything is working fine with GET requests and POST requests with text/html data.
I have problem to foward request with multipart/form-data data, in particular i have data both within the $_POST and $_FILES global variables.
How should I use these two variables to forward the request to the server?
You will need to store the files in your server first, then use a code like this one :
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
I try to make a post request with php and curl. Here is my code
//PHP 5.3.5 and curl: 7.18.2
$ch = curl_init();
if(!empty($save_cookie)){
curl_setopt($ch, CURLOPT_COOKIEJAR, $save_cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $save_cookie);
}else{
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/post.php');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_NOBODY, !$body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
return false;
}
curl_close($ch);
return $postResult;
In http://localhost/post.php, I write
print_r($_SERVER);
The result return of curl is always
[REQUEST_METHOD] => GET
Remove the CURLOPT_NOBODY option and it will work. Or place it above the CURLOPT_POST line.
I think I have encountered this once, when trying to get just the header of a response. Setting
curl_setopt($ch, CURLOPT_NOBODY, true);
effectively instructs curl to issue a HEAD request, which is not a POST request. I think there is no way to just get the header from a POST (and just drop the connection after receiving the header). As a side effect, setting CURLOPT_NOBODY to false sets the request type to GET...
Do you really need the CURLOPT_NOBODY flag?
Try to move the
curl_setopt($ch, CURLOPT_NOBODY, !$body);
line right before the
curl_setopt($ch, CURLOPT_POSTFIELDS, $pars);
line.
There's an interesting post at the curl/set_opt page, shedding some light on this behaviour:
If your POST data seems to be disappearing (POST data empty, request
is being handled by the server as a GET), try rearranging the order of
CURLOPT_POSTFIELDS setting with CURLOPT_NOBODY. CURLOPT_POSTFIELDS has
to come AFTER CURLOPT_NOBODY setting because if it comes after it
wipes out the header that tells your URL target that the
request is a POST not a GET.