I want to download a repository file from github after that the user doing an action. The code will not work as expected. A .zip file is created when the code run but the resulting zip file is blank. How I can fix this?
$url = "https://github.com/$u/$repo/archive/master.zip";
$ch = curl_init();
$f = fopen(__DIR__.'/master.zip', 'w+');
$opt = [
CURLOPT_URL => $url,
CURLOPT_FILE => $f,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
];
curl_setopt_array($ch, $opt);
$file = curl_exec($ch);
curl_close($ch);
fclose($f);
Remove CURLOPT_RETURNTRANSFER => true from your code or put it before CURLOPT_FILE.
This is known interaction as CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set.
Related
I am using php curl to copy a folder to another folder (to become a subfolder inside the second folder). Here is the curl option array (CODE value hidden) followed by the curl operations:
array (
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_VERBOSE => false,
CURLOPT_HEADER => false,
CURLINFO_HEADER_OUT => true,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_FOLLOWLOCATION => false,
CURLOPT_HTTPHEADER =>
array (
'Authorization: Bearer CODE',
'Content-Type: application/json',
'Content-Length: '.$len,
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => {"name":"Newfolder","parent":{"id":"$id"}}
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
The result is '1'.
You need to set
CURLOPT_RETURNTRANSFER => true
Quoting from PHP Manual
true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly.
I'm running PHP 5.6.36 running on windows server 12R2 under IIS 8.5
Whenever I call this code I get an "error code: 6" echoed and an empty $responseText, although I'm able to reach this address with the browser.
I've tried toggling the CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to true settings and using https:// addresses, but to no avail. Don't get why I can reach these addresses through the browser, but not from the command line.
function genericGet($url,$userName,$passWord){
//overriding $url for the purposes of test
$url = "http://www.google.com";
$reqDefaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => false,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERNAME => $userName,
CURLOPT_PASSWORD => $passWord
);
$curlReq = curl_init();
$responseText = curl_exec($curlReq);
echo "error code:".curl_errno($curlReq)."\r\n";
curl_close($curlReq);
return $responseText;
}
Ok so I finally worked this out. There was an application proxy running on the server. I checked the internet settings in Firefox and found the proxy address then added the CURLOPT_PROXY setting to my setopt array and it worked. Final code looked like this:
function genericGet($url,$userName,$passWord){
//overriding $url for the purposes of test
$url = "http://www.google.com";
$reqDefaults = array(
CURLOPT_URL => $url,
CURLOPT_POST => false,
CURLOPT_HEADER => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERNAME => $userName,
CURLOPT_PASSWORD => $passWord,
CURLOPT_PROXY => 'proxyaddress:8080'
);
$curlReq = curl_init();
$responseText = curl_exec($curlReq);
echo "error code:".curl_errno($curlReq)."\r\n";
curl_close($curlReq);
return $responseText;
}
Currently I have the following code.
error_reporting(E_ALL);
ini_set('display_errors', 1);
include('simple_html_dom.php');
$df = upload('username','password','song.mp3','song.mp3');
$df = json_decode($df, true);
function upload($login,$password,$filename,$file) {
$ch = curl_init();
$postData = array(
'op' => 'login',
'login' => $login,
'password' => $password,
'returnto' => '/'
);
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/login.html',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://dopefile.pk/?op=upload");
$output = curl_exec($ch);
$html = str_get_html($output);
$ret1 = $html->find('form[id=uploadfile]');
$ret = $html->find('form[id=uploadfile] input');
foreach($ret AS $r) {
$newPost[$r->name]=$r->value;
}
$newPost['file_0'] = '#'.$file.';filename='.$filename;
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://dopefile.pk/cgi-bin/upload.cgi?upload_type=file',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $newPost,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEJAR => 'cookie.txt'
));
return $output = curl_exec($ch);
}
Which is trying to upload to the file sharing website the file song.mp3 which is within my directory, and then return the file sharing websites link (if you download the simple_html_dom.php files and try and run the code above, you can see that it returns no errors or returns no file sharing link + does not upload to my account on the website...
The cookies file updates with the cookies that show I successfully logged in, how ever I can't get the actual file link returned can anyone help me please?
I have php a code and can't understand why the php script creates only 4 files, but without the data. If I use curl only for one url without foreach loop all will be ok. Any thoughts?
$years = array('2012', '2013', '2014', '2015');
foreach($years as $year)
{
$url = "http://lpo.dt.navy.mil/data/DM/Environmental_Data_Deep_Moor_{$year}.txt";
$fp = fopen(base_path() . "/database/rawdata/{$year}.txt", 'w');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FAILONERROR => 1
));
curl_exec($curl);
if (!curl_errno($curl))
{
$info = curl_getinfo($curl);
Log::info("File created {$year}.txt");
Log::notice("Evaluation of this script was {$info['total_time']} seconds!");
}
else
{
Log::error("cURL ERROR " . curl_error($curl));
}
curl_close($curl);
fclose($fp);
}
See joeterranova's comment on the PHP site:
It appears that setting CURLOPT_FILE before setting
CURLOPT_RETURNTRANSFER doesn't work, presumably because
CURLOPT_FILE depends on CURLOPT_RETURNTRANSFER being set
.
Therefore you should change your curl_setopt_array to the following:
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FILE => $fp,
CURLOPT_VERBOSE => 1,
CURLOPT_FAILONERROR => 1
));
i'm stuck to try handle a random name and value of form html to post data through curl.
here my code.
function grapvalue($html){
//parse content to grap name and value random
}
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => grapvalue($html)
)
$init = curl_init("site.com")
curl_setopt_array($init,$opt)
$html = curl_exec($init)
I have to execute the function curl_exec () twice, first I have to execute the curl_exec function to retrieve the html to get the names and random values, then send the data.
how do I resolve this?
Here is the full solution:
//First, we make a request to grab the variables
function grapvalue(){
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
);
$init = curl_init("site.com");
curl_setopt_array($init,$opt);
$html = curl_exec($init);
//parse content to grap name and value random
}
//Than, just call grapvalue() without any parameters
$opt = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt'
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => grapvalue()
);
$init = curl_init("site.com");
curl_setopt_array($init,$opt);
$html = curl_exec($init);
Tip: you may prefer using CURLOPT_COOKIE rather than CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR.
Why you don't create a function cURL(), call the function, get the $html and call again with the post values?