Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I need the same result as when calling curl from terminal/consol, even if the answer is 302. Unfortunately I didn't get any data during my attempts.
$ curl bretten.work
Moved Permanently.
Your server seems to block requests if no user agent is set. Try this:
<?php
$ch = curl_init('http://bretten.work/');
curl_setopt($ch, CURLOPT_USERAGENT, 'curl/7.68.0');
curl_exec($ch);
To diagnose further problems try to compare curl -vvv https://example.com/ with this:
<?php
$ch = curl_init('http://example.com/');
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$cv = curl_version();
$useragent = 'curl';
if (isset($cv['version'])) {
$useragent .= '/' . $cv['version'];
}
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$streamVerboseHandle = fopen('php://temp', 'w+');
curl_setopt($ch, CURLOPT_STDERR, $streamVerboseHandle);
$result = curl_exec($ch);
if ($result === false) {
printf("cUrl error (#%d): %s<br>\n",
curl_errno($ch),
htmlspecialchars(curl_error($ch)))
;
}
rewind($streamVerboseHandle);
$verboseLog = stream_get_contents($streamVerboseHandle);
echo "cUrl verbose information:\n",
"<pre>", htmlspecialchars($verboseLog), "</pre>\n";
To make php curl follow redirects, the FOLLOWLOCATION setting is needed, as such:
<?php
$curl = curl_init('http://bretten.work/');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_exec($curl);
See https://evertpot.com/curl-redirect-requestbody/ for more options.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Well, here's my cURL script inside bash that works without any issues!
#!/bin/bash
fileid="1yvklOFopnep8twiqAQecmMUoAbQVzU0r"
filename="MyFile.mp4"
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename}
However I'm trying to rewrite this into a simple PHP script, although it appears not to be working correctly; here's the code:
<?php
define('FILENAME', 'MyFile.mp4');
define('FILE_ID', '1yvklOFopnep8twiqAQecmMUoAbQVzU0r');
$GlobalFileHandle = null;
function get_confirm($id)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://drive.google.com/uc?export=download&id=".$id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
preg_match_all("/confirm=([0-9A-Za-z]+)&/", $result, $output_array);
return $output_array[1][0];
}
function get_file($id, $confirm)
{
global $GlobalFileHandle;
$GlobalFileHandle = fopen(FILENAME, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://drive.google.com/uc?export=download&confirm='.$confirm.'&id='.$id);
curl_setopt($ch, CURLOPT_FILE, $GlobalFileHandle);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'curlWriteFile');
curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
fclose($GlobalFileHandle);
}
function curlWriteFile($cp, $data)
{
global $GlobalFileHandle;
return fwrite($GlobalFileHandle, $data);
}
$confirm_code = get_confirm(FILE_ID);
echo "We got our confirm code! ".$confirm_code;
get_file(FILE_ID, $confirm_code);
However it appears the file is not being downloaded & the MyFile.mp4 remains empty?
You seem to mix several options in an invalid way here.
Since you set CURLOPT_RETURNTRANSFER the data is returned by curl_exec().
So
$data = curl_exec($ch);
fwrite($GlobalFileHandle, $data);
should do the trick. If you want to use the callbacks, do not set CURLOPT_RETURNTRANSFER at all.
Another option is, to set CURLOPT_FILE to write the data directly to a file handle (don't set CURLOPT_RETURNTRANSFER either then):
curl_setopt(CURL_FILE, $GlobalFileHandle);
Furthermore, you need to set the CURLOPT_COOKIEFILE to your cookiejar to have the cookies read correctly. The CURLOPT_COOKIEJAR option only sets the file where to store cookies to. You need both, so add:
curl_setopt(CURL_COOKIEFILE, 'cookies.txt');
For more details, refer to the curl_setopt PHP manpage
Seems like a curl_exec is missing in function get_file.
i know this has been asked many many times before,but this time should be the last time because the solution should be universaland unique that anyone can use the code anywhere in the project.
So the question : How to get any website header using curl in the same way as get_headers which produce an array.
I know i am answering my own question but because i have been working on the code myself.I was looking at some answers on stackoverflow and other websites,so i'm coming with simple code which give the universal and unique result.
I hope i don't have to explane it how to use the code because it's made by simple logic.
<?php
function curl_get_headers($base_url){
if(ini_get('allow_url_fopen')){
$result_header = get_headers($base_url);
}elseif(in_array('curl', get_loaded_extensions())){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
if(strpos($base_url,'https') !== false){
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
preg_match_all('/(.*)/',$response,$results);
foreach($results AS $result_key=>$result_array){
foreach($result_array AS $value){
if(strlen($value)>=10){
$string = preg_replace('/\s+$/','',$value);
$result[$result_key][] = $string;
}
}
}
$result_header = end($result);
}else{
/* Because we got this far, tell the user what to do! */
die('<h1 align="center">Server error : allow_url_fopen and curl is not enabled,<br />please ask your webhosting to enable one of the option!</h1>');
}
return $result_header;
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to get Json Data from a https url.
I have tested and working code to do just this for http but I am yet to find a working version for https. This needs to be secure code.
This is my http version that works:
<?php
$key = "admin"; //user
$secret = "admin"; //pass
$api_ep = "http://$Hostname:$Port/$address";
if ($key && $secret){
$curl_opts = array(
CURLOPT_HTTPHEADER=>array("Accept: application/json"),
CURLOPT_VERBOSE=>false,
CURLOPT_HEADER=>false,
CURLOPT_POST=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_HTTPAUTH=>CURLAUTH_DIGEST,
CURLOPT_USERPWD=>$key.":".$secret,
CURLOPT_URL=> $api_ep
);
}
function disp($opts,$var){
$ch = curl_init();
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
// $array_resp = json_decode($raw_resp);
//print_r($array_resp);
print_r($raw_resp);
curl_close($ch);
//$array = json_decode($raw_resp, true);
//print_r($array_resp);
//disp_table($array_resp, $var);
}
disp($curl_opts,$Type);
?>
If you don't have any sensitive data you can try setting CURLOPT_SSL_VERIFYPEER to FALSE
Else you will have to verify the certificate.
Since Curl doesn't have built-in root certificates. You need to explicitly point it to a cacert.pem file:
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/cacert.pem');
Without this, curl cannot verify the certificate sent back via ssl. This same root certificate file can be used every time you use SSL in curl.
You can get the cacert.pem file here: http://curl.haxx.se/docs/caextract.html
Note:
Setting CURLOPT_SSL_VERIFYPEER to false allows for man-in-the-middle-attacks. rmckay at webaware dot com dot au warns for this on nl3.php.net/manual/en/function.curl-setopt.php and gives an alternative solution that works: downloading a CA root certificate bundle at the curl website and saving it on your server. See at the given site, scroll down to the user comments
This code below works for https.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $basicAuthUrl);
$header = array();
$header[] = 'Content-length: 29';
$header[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$header[] = 'Authorization: Basic ' . $keyAndSecretEncoded;
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
Add the following to disable SSL verification.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Curl SSL fails to verify self signed certificates and hence, you will not be able to connect with https hosts if they are using self signed certificates.
function disp($opts,$var){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($ch, $opts);
$raw_resp = curl_exec($ch);
print_r($raw_resp);
curl_close($ch);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
how are you?
i have a non expected result ,
it should return "1" when login successfuly , and "0" when login error,
and it always return "0"
<?php
$username = 'admin';
$password = 'admin';
$loginUrl = 'http://localhost/wordpress/wp-login.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'log='.$username.'&pwd='.$password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/wordpress/wp-admin/index.php');
$content = curl_exec($ch);
curl_close($ch);
file_put_contents('3.php', $content);
// if "Password" appear then login error;
echo preg_match("/Password/",file_get_contents("3.php"));
?>
and Thank you :)
Writing the output to a file is not necessary for this and will waste I/O. The second request to the index of the admincp can also saved for checking the login only because wordpress will print the loginform again when the login failed. So I modified your example like the following:
$username = 'admin';
$password = 'admin';
$loginUrl = 'http://localhost/wordpress/wp-login.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'log=' . $username . '&pwd=' . $password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
curl_close($ch);
if(strpos($store, 'loginform') !== false) {
echo 'Login not correct';
}else {
echo 'Login ok';
}
But for me this seems not to be a good way of doing this. Depending on where you want to integrate this checks it will be a better solution to write a small plugin for the check or maybe integratin wordpress: http://codex.wordpress.org/Integrating_WordPress_with_Your_Website Then you can use the wordpress-functions to check the credentials like https://codex.wordpress.org/Function_Reference/wp_check_password
This question already has answers here:
Get file content from URL?
(5 answers)
Closed 4 years ago.
I wanna put the content of a URL in a string and the process it. However, I have a problem.
I get this error:
Warning: file_get_contents(http://www.findchips.com/avail?part=74ls244) [function.file-get-contents]: failed to open stream: Redirection limit reached,
I have heard this comes due to page protection and headers, cookies and stuff.
How can I override it?
I also have tried alternatives such as fread along with fopen but I guess I just don't know how to do this.
Can anyone help me please?
original answer moved to this topic .
Use cURL,
Check if you have it via phpinfo();
And for the code:
function getHtml($url, $post = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if(!empty($post)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
Try using cURL instead. cURL implements a cookie jar, while file_get_contents doesn't.