Save external files by php - php

I have an array with urls, like:
[1] = http://site.com/1.pdf
[2] = http://site.com/234234234.png
[3] = http://site.com/archive.zip
[4] = http://site.com/1f41f.anyformat
[5] = http://site.com/file.txt
How do I save them to some folder on my ftp by PHP?
Names of the files should not change.

Here's a simple example:
$urls = array('url1', 'url2');
foreach($urls as $url) {
$data = file_get_contents($url);
file_put_contents('/path/to/folder/'.basename($url), $data);
}

Maybe this will help you solve the question
function remote_merge($sourceurl,$targetftp){
$ch = curl_init ($sourceurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
$tempfile = "/path/to/temp/".basename(parse_url($sourceurl, PHP_URL_PATH));
if(file_exists($tempfile)){
unlink($tempfile);
}
$fp = fopen($tempfile,'x');
fwrite($fp, $rawdata);
fclose($fp);
$ch = curl_init();
$fp = fopen($tempfile, "rb");
curl_setopt($ch, CURLOPT_URL, $targetftp);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($tempfile));
$error = curl_exec($ch);
// check $error here to see if it did fine or not!
curl_close($ch);
}
Use this to tryout the remote_merge function
$sourceurls = array(
"http://site.com/1.pdf",
"http://site.com/234234234.png",
"http://site.com/archive.zip",
"http://site.com/1f41f.anyformat",
"http://site.com/file.txt"
);
foreach($sourceurl as $sourceurls){
$filename = basename(parse_url($sourceurl, PHP_URL_PATH);
$targetftp = "ftp://${ftpuser}:${ftppasswd}#${ftpserver}${ftppath}/$filename";
remote_merge($sourceurl,$targetftp)
}

193 questions, 3 answers... wow.
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close ($ch);
return curl_exec($ch);
}
$files = array();
$dir = "dir/";
foreach($files as $file){
$name = explode("/", $file);
$name = end($name);
$contents = curl($file);
file_put_contents($dir.$name, $contents);
}

Related

Retrieve ALL video datas with Youtube API V3 PHP

I am having issues with YouTube Data API. I'm working on a PHP function to retrieve all videos information in JSON format from a YouTube channel, and then put that into a file.
So far I managed to call the API and retrieve the first 50 videos info; am also knowing the method to create the file.
I know that I have to use the nextPageToken parameter to loop the function until there is no nextPageToken, but I'm stuck. I've seen a lot of similar posts with this problem, but none of them really helped me.
To sum up, I need help to:
use nextPageToken to collect information after 50 videos;
collect this information each turn of the loop.
So far the code I found and used:
function youtube_search($API_key, $channelID, $max_results, $next_page_token=''){
$myQuery = "https://www.googleapis.com/youtube/v3/search?key=".$API_key."&channelId=".$channelID."&part=snippet,id&order=date&maxResults=".$max_results;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $myQuery);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
if(!empty($data->nextPageToken)){
return youtube_search($API_key, $channelID, $max_results, $searchResponse['nextPageToken']);
}
}
youtube_search($API_key, $channelID, $max_results, $next_page_token='');
Thank you for helping (am a humble beginner -_-).
I asked a friend and he helped me with my issue. This code didn't work because I didn't have the right arguments... and many other problems. This code works to retrieve videos info from a channel and write it in a JSON file
<?php
$API_key = 'API_key';
$channelID = 'channelID';
$max_results = 50;
$table = array();
$file_name = 'all-videos.json';
function youtube_search($API_key, $channelID, $max_results, $next_page_token,$videos,$file){
$dataquery = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=".$channelID."&maxResults=".$max_results."&order=date&pageToken=".$next_page_token."&key=".$API_key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $dataquery);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
foreach ($data->items as $item){
array_push($videos,$item);
}
$content = json_encode($videos);
file_put_contents($file, $content);
if(!empty($data->nextPageToken)){
youtube_search($API_key, $channelID, $max_results, $data->nextPageToken, $videos, $file);
}
}
youtube_search($API_key, $channelID, $max_results, $next_page_token='', $table, $file_name);
?>
(Disclaimer: this is not an actual answer to OP's question, but only a code refactoring of his own answer below.)
Here is your code refactored such that to have a do...while loop instead of tail recursion:
function youtube_search_paginated(
$API_key, $channelID, $max_results, $next_page_token, $videos, $file)
{
$dataquery = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=".$channelID."&maxResults=".$max_results."&order=date&key=".$API_key;
if (!empty($next_page_token))
$dataquery .= "&pageToken=".$next_page_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $dataquery);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
foreach ($data->items as $item){
array_push($videos,$item);
}
$content = json_encode($videos);
file_put_contents($file, $content);
return $data->nextPageToken;
}
function youtube_search(
$API_key, $channelID, $max_results, $videos, $file)
{
$next_page_token = NULL;
do {
$next_page_token = youtube_search_paginated(
$API_key, $channelID, $max_results, $next_page_token, $videos, $file);
} while (!empty($next_page_token));
}
youtube_search($API_key, $channelID, $max_results, $table, $file_name);

Error 0 byte when save image to local or other server

function grab_image($url, $saveto){
$url = $url;
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw = curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto, 'w');
fwrite($fp, $raw);
fclose($fp);
}
$link = 'https://images-na.ssl-images-amazon.com/images/I/415lKuJC%2B2L.jpg';
grab_image($link, '/tmp/415lKuJC%2B2L.jpg');
Error when save to local file (0 byte), I think this link have special character is %2B
Try adding file name in function call, like following:
grab_image($link, "/tmp/new_file_name.jpg");
alternative solution to grab file with original filename:
function grab_image($url, $savePath = getcwd())
{
$path = $savePath.'/'.basename($url);
if (file_exists($path)) {
unlink();
}
copy($url, $path);
}
$link = 'https://images-na.ssl-images-amazon.com/images/I/415lKuJC%2B2L.jpg';
grab_image($link);

php json_decode is not working Properly

When i am Decoding using commented "$jsonString" String it is working very well.
But after using curl it is not working, showing Null.
Please Help Me in this.
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
$url = 'http://ExampleStatus.php?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// $jsonString = '[{"branchname":"BHUBNESHWAR","consignee":"ICICI BANK LTD","currentstatus":"Delivered by : BHUBNESHWAR On - 25/07/2015 01:00","dlyflag":"Y","PODuploaded":"Not Uploaded"}]';
if ($jsonString != '') {
$json = str_replace(array('[', ']'), '', $jsonString);
echo $json;
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}
}
}
After curl I used following concept to get only JSON data from HTML Page.
1) fetchData.php
$url = 'http://DocketStatusApp.aspx?dkno=' . $dcktNo;
$myvars = '';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonString = curl_exec($ch);
// now get only value
$dom = new DOMDocument();
$dom->loadHTML($jsonString);
$thediv = $dom->getElementById('Label1');
echo $thediv->textContent;
2) JSONprocess.php
if (isset($_POST['dkno'])) {
$dcktNo = $_POST['dkno'];
ob_start(); // begin collecting output
include_once 'fetchData.php';
$result = ob_get_clean(); // Completed collecting output
// Now it will show & take only JSON Data from Div Tag
$json = str_replace(array('[', ']'), '', $result);
$obj = json_decode($json);
if (is_null($obj)) {
die("<br/>Invalid JSON, don't need to keep on working on it");
} else {
$podStatus = $obj->PODuploaded;
}
}

Submit xml to GSA with PHP / foreach loop

Trying to submit to GSA (google search applicance). Works fine for 1 xml file. But im trying to loop through all files in a directory and submit to the gsa with a loop but cannot get it working.
<?php
$target_url = 'http://1.1.1.1:19900/xmlfeed';
$header = array('Content-Type: multipart/form-data');
$directory = 'xml';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
foreach (scandir($directory) as $file) {
if ('.' === $file) continue;
if ('..' === $file) continue;
}
//print $file;
$fields = array(
'feedtype'=>'incremental',
'datasource'=>'testing',
'data'=>file_get_contents(realpath($file))
//'data'=>file_get_contents(realpath('test.xml')) //works fine
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_TIMEOUT,120);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$return = curl_exec($ch);
if (curl_errno($ch)) {
$msg = curl_error($ch);
}
curl_close ($ch);
echo $return;
?>
Tried adding a foreach loop but that gives me an error that the file is empty.
You should do a request for each file. Put your curl execution in the foreach loop and it should be fine :
<?php
$target_url = 'http://1.1.1.1:19900/xmlfeed';
$header = array('Content-Type: multipart/form-data');
$directory = 'xml';
if (! is_dir($directory)) {
exit('Invalid diretory path');
}
$files = array();
//List all files in the directory
foreach(glob($directory."/*.*") as $file) {
//Check the file extension
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext != 'xml') continue;
//Add the fields
$fields = array(
'feedtype'=>'incremental',
'datasource'=>'testing',
'data'=>file_get_contents($file)
);
//Post the request
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_TIMEOUT,120);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$return = curl_exec($ch);
if (curl_errno($ch)) {
$msg = curl_error($ch);
}
curl_close ($ch);
//Print the result for each file
echo "Result for " . basename($file) . " is : " . $return;
}
?>
It works fine for me. I hope it will help :)

Fogbugz XML_API PHP CURL File upload

I have built a php script which receives values in $_POST and $_FILES
I'm catching those values, and then trying to use CURL to make posts to FogBugz.
I can get text fields to work, but not files.
$request_url = "http://fogbugz.icarusstudios.com/fogbugz/api.php";
$newTicket = array();
$newTicket['cmd'] = 'new';
$newTicket['token'] = $token;
$newTicket['sPersonAssignedTo'] = 'autobugz';
$text = "\n";
foreach( $form as $pair ) {
$text .= $pair[2] . ": " . $pair[0] . "\n";
}
$text = htmlentities( $text );
$newTicket['sEvent'] = $text;
$f = 0;
foreach ($_FILES as $fk => $v) {
if ($_FILES[$fk]['tmp_name'] != '') {
$extension = pathinfo( $_FILES[$fk]['name'], PATHINFO_EXTENSION);
//only take the files we have specified above
if (in_array( array( $fk, $extension ) , $uploads)) {
$newTicket['File'.$f] = $_FILES[$fk]['tmp_name'];
//echo ( $_FILES[$fk]['name'] );
//echo ( $_FILES[$fk]['tmp_name'] );
//print $fk;
//print '<br/>';
//print_r( $v );
}
}
}
$ch = curl_init( $request_url );
$timeout = 5;
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newTicket );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
To upload files with CURL you should prepend a # to the path, see this example:
$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, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
Taken from http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html.
The other answer -- for FogBugz reasons only --
$f cannot be set to 0 initially. It must be 1, so the files go through as File1, File2, etc.
The # symbol is also key.

Categories