[ZipStream]using zipstream downloading many urls - url

I have tried to use ZipStream and I figured that when the urls are small like 5 urls,it works well,but when the urls are bigger, the brower turns around and around and I can't get the zip file.Has anybody met same question before? Many thanks.Below is my code.I'm using mac os and debug the code on phpstorm using localhost and port 63342.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use ZipStream\ZipStream;
// Create new Zip
$zip = new ZipStream("hi.zip");
// add a lot of http urls
$urls = [
'http://img31.mtime.cn/pi/2014/10/22/092931.12614666_1000X1000.jpg',
'http://img31.mtime.cn/pi/2014/10/22/092931.79193700_1000X1000.jpg',
];
foreach($urls as $url) {
// Create Temp File
$fp = tmpfile();
// Download File
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
// Force to write all data
fflush($fp);
// Rewind to the beginning, otherwise the stream is at the end from the start
rewind($fp);
// Find out a file name from url
// In this case URL
http://img31.mtime.cn/pi/2014/10/22/092931.12614666_1000X1000.jpg will yield
// /pi/2014/10/22/092931.12614666_1000X1000.jpg as file path
$filename = parse_url($url, PHP_URL_PATH);
// Add File
$zip->addFileFromStream($filename, $fp);
// Close the Temp File
fclose($fp);
}
// Finish ZIP
$zip->finish();

well,I have succeffully downloaded 1.3G zip file from the brower using the above code. At first, I can't get the zip file because I use phpstorm inner Interpreter,later I changed to apache server and successed. Thanks for zipstream's author Jonatan Männchen.

Related

User Channels / Unknown Subresource

The newly added (05/02) User Channels resource seems to be dead. When I attempt to access this data I always receive an error that says "Unknown subresource channels".
Can anyone confirm that this functionality is actually working?
I've tried retrieving it a couple of different ways:
$channels = $this->client->chat
->services($this->ipmSid)
->users($memberId)
->channels
->read(); // I've also tried ->fetch();
and then I tried using the link that is provided when you fetch a member and feeding that directly into curl like this:
$member = $this->client->chat
->services($this->ipmSid)
->users($user_id)
->fetch();
$url = $member->links['user_channels'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($process, CURLOPT_USERPWD, $this->sid . ":" . $this->token);
$resp = curl_exec($ch);
Both ways result in a 500 error "Twilio Exception Unknown Subresource Channels".
Thanks for any tips!
It is not documented well. You need to use userChannels.
channels = $this->client->chat
->services($this->ipmSid)
->users($memberId)
->userChannels
->read();

Hiding YouTube API for client using server

My inexperience has left me short of understanding how to hide an API Key. Sorry, but I've been away from web development for 15 years as I specialized in relational databases, and a lot has changed.
I've read a ton of articles, but don't understand how to take advantage of them. I want to put my YouTube API key(s) on the server, but have the client able to use them w/o exposure. I don't understand how setting an API Key on my server (ISP provided) enables the client to access the YouTube channel associated with the project. Can someone explain this to me?
I am not sure what you want to do but for a project I worked on I needed to get a specific playlist from YouTube and make the contents public to the visitors of the website.
What I did is a sort of proxy. I set up a php file contains the api key, and then have the end user get the YT content through this php file.
The php file gets the content form YT using curl.
I hope it helps.
EDIT 1
The way to hide the key is to put it in a PHP file on the server.
This PHP file will the one connecting to youtube and retrieving the data you want on your client page.
This example of code, with the correct api key and correct playlist id will get a json file with the 10 first tracks of the play list.
The $resp will have the json data. To extract it, it has to be decoded for example into an associative array. Once in the array it can be easily mixed in to the html that will be rendered on the client browser.
<?php
$apiKey = "AIza...";
$results = "10";
$playList = "PL0WeB6UKDIHRyXXXXXXXXXX...";
$request = "https://www.googleapis.com/youtube/v3/playlistItems?part=id,contentDetails,snippet&maxResults=" . $results .
"&fields=items(contentDetails%2FvideoId%2Cid%2Csnippet(position%2CpublishedAt%2Cthumbnails%2Fdefault%2Ctitle))" .
"&playlistId=" . $playList .
"&key=" . $apiKey;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $request,
CURLOPT_SSL_VERIFYPEER => false
));
$resp = curl_exec($curl);
if (curl_errno($curl)) {
$status = "CURL_ERROR";
}else{
// check the HTTP status code of the request
$resultStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($resultStatus == 200) {
$status = "OK";
//Do something with the $resp which is in JSON format.
//Like decoding it into an associative array
} else {
$status = "YT_ERROR";
}
}
curl_close($curl);
?>
<html>
<!-- your html here -->
</html>
Note: CURLOPT_SSL_VERIFYPEER is set to false. This is in development. For prod it should be true.
Also note that using the api this way, you can restrict the calls to your api key bounding them to your domain. You do that in the googla api console. (Tip for production)

PHP: upload file from one server to another server

I'm developing a website in a server and the storage is in another server and I have to deal with that somehow. I nerve experience this case and I found that the solution is to use curl.
Kindly explain to me how to use Curl in detail from zero.
Update:
I use the following code to test if cURL is installed and enabled:
<?PHP
phpinfo();
$toCheckURL = "http://board/accSystem/webroot/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $toCheckURL);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
$data = curl_exec($ch);
curl_close($ch);
preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
$code = end($matches[1]);
if(!$data) {
echo "Domain could not be found";
} else {
switch($code) {
case '200':
echo "Page Found";
break;
case '401':
echo "Unauthorized";
break;
case '403':
echo "Forbidden";
break;
case '404':
echo "Page Not Found";
break;
case '500':
echo "Internal Server Error";
break;
}
}
?>
Here is the result:
And I got (Page found) message
Now I can use cURL without worry, right ?
Note:
Both servers are local
As a PHP developer, you may already be familiar with PHP's most handy file system function, fopen. The function opens a file stream and returns a resource which can then be passed to fread or fwrite to read or write data. Some people don't realize though that the file resource doesn't necessarily have to point to a location on the local machine.
Here's an example that transfers a file from the local server to an ftp server:
$file = "filename.jpg";
$dest = fopen("ftp://username:password#example.com/" . $file, "wb");
$src = file_get_contents($file);
fwrite($dest, $src, strlen($src));
fclose($dest);
A listing of different protocols that are supported can be found in Appendix M of the PHP manual. You may wish to use a protocol that employs some encryption mechanism such as FTPS or SSH depending on the network setup and the sensitivity of the information you’re moving.
The curl extension makes use of the Client URL Library (libcurl) to transfer files. The logic of implementing a curl solution generally follows as such: first initialize a session, set the desired transfer options, perform the transfer and then close the session.
Initializing the curl session is done with the curl_init function. The function returns a resource you can use with the other curl functions much as how a resource is obtained with fopen in the file system functions.
The upload destination and other aspects of the transfer session are set using curl_setopt which takes the curl resource, a predefined constant representing the setting and the option’s value.
Here's an example that transfers a file from the local host to a remote server using the HTTP protocol's PUT method:
$file = "testfile.txt";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://example.com/putscript");
curl_setopt($c, CURLOPT_USERPWD, "username:password");
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));
$fp = fopen($file, "r");
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_exec($c);
curl_close($c);
fclose($fp);
A list of valid options for curl can be found in the php documentation.
The ftp extension allows you to implement client access to ftp servers. Using ftp to transfer a file is probably overkill when options like the previous two available... ideally this extension would be best used more advanced functionality is needed.
A connection is made to the ftp server using ftp_connect. You authenticate your session with the ftp server using ftp_login by supplying it a username and password. The file is placed on the remote server using the ftp_put function. It accepts the name of the destination file name, the local source file name, and a predefined constant to specify the transfer mode: FTP_ASCII for plain text transfer or FTP_BINARY for a binary transfer. Once the transfer is complete, ftp_close is used to release the resource and terminate the ftp session.
$ftp = ftp_connect("ftp.example.com");
ftp_login($ftp, "username", "password");
ftp_put($ftp, "destfile.zip", "srcfile.zip", FTP_BINARY);
ftp_close($ftp);
We are using same use case like you. we have two server : Application server and Storage server.Where application servers contains methods and UI part and storage server is where we upload files.Following is what we are using for file upload , that is working properly :
1) You have to add one php file that contains following methods on your storage server :
<?php
switch ($_POST['calling_method']) {
case 'upload_file':
echo uploadFile();
break;
case 'delete':
return deleteFile();
break;
}
function uploadFile() {
$localFile = $_FILES['file']['tmp_name'];
if (!file_exists($_POST['destination'])) {
mkdir($_POST['destination'], 0777, true);
}
$destination = $_POST['destination'] . '/' . $_FILES['file']['name'];
if (isset($_POST['file_name'])) {
$destination = $_POST['destination'] . '/' . $_POST['file_name'];
}
$moved = move_uploaded_file($localFile, $destination);
if (isset($_POST['file_name'])) {
chmod($destination, 0777);
}
$result['message'] = $moved ? 'success' : 'fail';
echo json_encode($result);
}
function deleteFile() {
if (file_exists($_POST['file_to_be_deleted'])) {
$res = unlink($_POST['file_to_be_deleted']);
return $res;
}
return FALSE;
}
?>
2) On your application server.
Create a method that will pass $_FILES data to storage server.
$data = array(
'file' => new CURLFile($_FILES['file']['tmp_name'],$_FILES['file']['type'], $_FILES['file']['name']),
'destination' => 'destination path in which file will be uploaded',
'calling_method' => 'upload_file',
'file_name' => 'file name, you want to give when upload will completed'
);
**Note :CURLFile class will work if you have PHP version >= 5**
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PATH_FOR_THAT_PHP_FILE_ON_STORAGE_SERVER_FILE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']);
$response = curl_exec($ch);
if (curl_errno($ch)) {
$msg = FALSE;
} else {
$msg = $response;
}
curl_close($ch);
echo $msg;
In this way using CURL it will call storage server file method and upload files and in same way you can call method to delete file.
Hope this will help.
You may google, there are lots of tutorials on how to work with curl. In your case you need to develop two parts on both servers:
reception form, which will await for file being uploaded (receiver)
curl upload script (sender)
The sender script could be alike:
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '#/path/to/file.txt'));
curl_setopt($ch, CURLOPT_URL, 'http://yoursecondserver.com/upload.php');
curl_exec($ch);
curl_close($ch);
How to receive file on the second server you may read up in the PHP manual, lots of examples http://php.net/manual/en/features.file-upload.php
Potentially you might want to use FTP connection, if have it installed on your servers, it might be even easier.
In this case You can call PHP script on remote server with path to file on local server.
Then on remote server using file_get_contents save the file.
Example on local server:
<?php
file_get_contents('http://somestaticfilesserver.com/download.php?file=path/to/some/file.png')
and download.php can look like this:
<?php
$remoteHost = 'http://serversendingfile.com';
$rootPath = realpath(__DIR__) . DIRECTORY_SEPARATOR . 'uploaded' . DIRECTORY_SEPARATOR;
$filePath = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, filter_input_array(INPUT_GET)['file']);
$fileName = (basename($filePath));
$remoteFile = $remoteHost . $filePath;
$file = file_get_contents($remoteFile);
$absolutePath = str_replace($fileName, '', $rootPath . $filePath);
if (!is_dir($absolutePath)) {
mkdir($absolutePath, 0777, true);
}
if (file_put_contents($absolutePath . $fileName, $file, FILE_APPEND)) {
die('ok');
}
die('nok');
the given path will be kept relative to $rootPath
First, make sure the other server will accept your connections, look up Cross Domain Policy issues. Also, make sure that the file is publicly available (ie: you can download it by navigation to the URL with a standard browser).
Once everything is setup, you can use file_get_contents to get the contents of the file and file_put_contents to save it locally:
file_put_contents('file_name', file_get_contents('FILE_URL'));
Your other questions (delete, edit) are really a different beast and should most likely be handled in their own questions, as there is no way to do this from your website server alone for obvious security reasons.
You would need to expose an API on the storage server and hit that API from the website to make the storage do actions the appropriate actions.
You can upload file on another server using posting the file.
$request = curl_init('http://example.com/');
// send a file
curl_setopt($request, CURLOPT_POST, true);
curl_setopt(
$request,
CURLOPT_POSTFIELDS,
array(
'file' => '#' . realpath('example.txt')
));
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
echo curl_exec($request);
curl_close($request);
You can handle the Uploaded file using $_FILES['file']

how to pass oauth2 token to mashup filab?

I have followed the whole process to obtain an oauth2 access token from the Identity Manager (I get it) and I want to pass it to a web application developed on Filab Mashup but embedded in my own web.
In my Mashup application I need get the oauth token for to access Orion Context Broker information but I don't know how to pass it.
This is the code of my callback URL where I obtain the token:
<?php
//get the code from url
$code = $_GET["code"];
//print_r($code);
//application specific declarations
$domain = "www.talkysync.com";
$clientId = "my_client_ID";
$clientSecret = "my_client_secret";
//access token url
$url = 'https://account.lab.fiware.org/oauth2/token';
//payload params for the request token
$payload = 'grant_type=authorization_code&code='. $code .'&redirect_uri=http%3A%2F%2Fwww.talkysync.com%2Ffiware_login%2Fcallback.php';
//base64(client_id:client_secret)
$cadena = $clientId . ":" .$clientSecret;
$base = base64_encode($cadena);
//extra header for the request
$header = array("Content-Type: application/x-www-form-urlencoded", "Authorization: Basic ". $base);
//actual request implementation
$ch = curl_init($url);
curl_close($ch);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
;
//get the access token from the json response
$jsonData = json_decode($output,true);
$access_token = $jsonData["access_token"];
//start a session and set the access token to it
session_start();
$_SESSION["X-Auth-Token"] = $access_token;
$_SESSION["code"] = $code;
header("Location: fiware.php");
?>
And this is the code of fiware.php:
<?php
session_start();
if(!isset($_SESSION["X-Auth-Token"])){
header('Location: login.php');
}else{
header('Location: https://mashup.lab.fiware.org/ertonio/Talkykar?mode=embedded');
}
?>
But in the mashup application I always have an anonymous connection because I don't know how to pass it the token.
Thanks in advance.
Currently is not possible to create an embedded WireCloud URL including the credentials. Seems interesting and worth implementing it, but also seems a lot of work, so don't expect it to be implemented in a short period of time.
Regarding, accessing the orion context broker from WireCloud, take a look into section "3.2.1. Using Orion Context Broker" on the WireCloud's course available on the FIWARE Academy.

Getting feedburner subscriber count

Can someone tell me how can I get the feedburner subscribers count as text using php? I am trying the following, but it does not work as I think feedburner has changed the contents of the xml file.
Thanks for any help.
$whaturl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=USERNAME";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $whaturl);
$data = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
echo $fb;
Need to alter line 7 to:
$fb = $xml->feed->entry->attributes()->circulation;
//Much later edit
I actually have a couple of posts on my blog by now with code for fetching facebook / twitter / feedburner counts. Code with APC caching is here:http://www.glowingminds.co.uk/facebook-likes-twitter-follows-and-feedburner-count-in-php-with-curlsimplexml-deluxe/

Resources