How can I asynchronously download files with Guzzle 6? - php

I am trying to asynchronously download files with Guzzle 6, but the documentation seems vague and couldn't find any useful examples.
The thing I am not sure about is - how am I supposed to save the received data?
Currently I am doing it like this:
$successHandler = function (Response $response, $index) use ($files) {
$file = fopen($files[$index], 'a');
$handle = $response->getBody();
while (!$handle->eof()) {
fwrite($file, $handle->read(2048));
}
fclose($file);
};
Is this really asynchronous?
Since if we get into one callback and start looping, how can we get the data from the other ones at the same time?
Is there a more direct way to tell, when creating a Request, where should the response be stored? (or directly passing a stream for that).

The sink option should be your friend here:
$client->request('GET', '/stream/20', [
'sink' => '/path/to/file',
]);
For reference, see http://docs.guzzlephp.org/en/latest/request-options.html#sink.

use function GuzzleHttp\Psr7\stream_for;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Client;
$tmpFile = tempnam(sys_get_temp_dir(), uniqid(strftime('%G-%m-%d')));
$resource = fopen($tmpFile, 'w');
$stream = stream_for($resource);
$client = new Client();
$options = [
RequestOptions::SINK => $stream, // the body of a response
RequestOptions::CONNECT_TIMEOUT => 10.0, // request
RequestOptions::TIMEOUT => 60.0, // response
];
$response = $client->request('GET', 'https://github.com/robots.txt', $options);
$stream->close();
fclose($resource);
if ($response->getStatusCode() === 200) {
echo file_get_contents($tmpFile); // content
}

Related

Streaming a response in CakePHP

I want CakePHP to stream a download to the browser. The content of the stream is served via an API.
So, CakePHP makes a request to that API, gets a response with a file and must stream this response to the browser.
This is what I got so far:
public function getDownload() {
// do other things
$http = new Client([
'headers' => [
'accept' =>'application/octet-stream'
]
]);
$response = $http->get($this->url,[]);
// first try
// $stream = new CallbackStream(function () use ($response) {
// return $response;
// });
// $response = $response->withBody($stream);
// second try
// $stream = new CallbackStream($http->get($this->url,[])->getData());
// $response = $response->withBody($stream);
return $response;
}
With this setup I can download small files. The reason I need a stream is, because the API could send files up to 10GB. My guess is, that with $http->get CakePHP stores the whole response in memory. Thats why I'm getting a memory exhausted error.
I know I'm lacking a bit of understanding here. Any help is appreciated :)
Finally I found a solution:
public function getDownload($url) {
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"accept: application/octet-stream\r\n"
)
);
$context = stream_context_create($opts);
$response = new Response();
$file = fopen($url, 'r',false, $context);
$stream = new CallbackStream(function () use ($file) {
rewind($file);
fpassthru($file);
fclose($file);
});
$response = $response->withBody($stream);
return $response;
}

Cognitive Service Face api use local image - PHP

I am trying to upload an local image to the face API by using PHP, I use the sample code to call the api and it is fine when i am using JSON. but it get stuck when change to octet-stream. i use the below code by referencing "https://pear.php.net/manual/en/package.http.http-request2.request.php"
$image = "images/face-021.jpg";
$fp = fopen($image, 'r+');
$request->addUpload('stuff', $fp, 'custom.name', 'application/octet-stream');
and i get the below responds form the api.
stdClass Object
(
[error] => stdClass Object
(
[code] => InvalidImageSize
[message] => Image size is too small or too big.
)
)
Am i missing some step before passing the binary data to the api?
The image size is just 956 KB and the dimensions is 2448 x 2448
The Face API does not support multipart MIME payloads. You should instead send the image directly in the body of the request.
$image = 'images/face-021.jpg';
$fp = fopen($image, 'rb');
$request->setBody($fp);
$request->setHeader('Content-Type', 'application/octet-stream');
$request->setMethod(HTTP_Request2::METHOD_POST);
function microimg() {
//$GLOBALS['telegram']->ssendmessage($GLOBALS['userid'], $urlImage);
$request = new Http_Request2('https://westus.api.cognitive.microsoft.com/face/v1.0/detect');
$url1 = $request->getUrl();
$headers = array(`enter code here`
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxx'
);
$request->setHeader($headers);
$parameters = array(
// Request parameters
'returnFaceId' => 'true',
'returnFaceLandmarks' => 'false',
'returnFaceAttributes' => 'age',
);
$parameters = array();
$url1->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_POST);
//$request->setBody('{"url": "http://miladddddddddddddd.ml/fillter_image/photos/file_93.jpg" }');
$request->setBody('{"url": "'.$GLOBALS['fileurl'].'" }');
$err = $request->setBody;
try {
$response = $request->send();
echo $response->getBody();
$ed = $response->getBody();
$GLOBALS['telegram']->ssendmessage($GLOBALS['userid'],$ed);
$GLOBALS['telegram']->ssendmessage($GLOBALS['userid'], $GLOBALS['fileurl']);
} catch (HttpException $ex) {
echo $ex;
}
}
microimg($urlimg);

Create a JSON-formatted text file with my username and password?

I'm trying to use PHP to authenticate to a web service by posting to an authentication method with a .txt file containing my username and password formatted in JSON.
It's not working and I'm having a hard time figuring out why not.
Here's the code I'm trying to use. First there's a function I'm using to do the posting. Then I create a variable for my data file and another for my URL for the AUTH service.
<?php
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
// Let's get logged in and get an authkey
$url = 'http://service.com/auth';
$data= 'creds.txt';
$authkey = do_post_request($url, $data);
print_r($authkey);
?>
The text of my creds.txt file:
{
"auth": {
"username": "myusername",
"password": "mypassword"
}
}
What am I doing wrong? I'm getting an invalid data error. Do I need to use a full URL to the text file? Is the textfile not formatted properly?
The docs for the service only say that I need:
"a JSON-formatted text file with your username and password"
$data= 'creds.txt'; should be like:
$data= file_get_contents('creds.txt');
I don't see you open the creds.txt anywhere, I believe you should send in jSON format.
Without disclosing "service.com" or more details about the website itself it'll be hard to figure out a solution since it can be a number of things but you're never passing the actual data (only the file path).
Try:
$authkey = do_post_request($url, file_get_contents($data));

xml posting

i want to post an xml document to a url, using simple php code.
i have a javascript code but the javascript will not support cross domain, so i just want to do it with php.
does any one have a code for this to support me...
Take a look at SimpleXML: http://us2.php.net/simplexml
Handling HTTP messaging in PHP is quite straightforward using the PECL HTTP classes.
In your instance you want to issue an HTTP request (that's a client->server message). Thankfully the HttpRequest::setPostFiles simplifies the process of including file content in an HTTP request. Refer to the PHP manual page (previous link) for specifics.
Unfortunately the manual pages for the HTTP classes are a bit sparse on details and it's not fully clear what the arguments for HttpRequest::setPostFiles should be, but the following code should get you started:
$request = new HttpRequest(HttpMessage::HTTP_METH_POST);
$request->setPostFiles(array($file));
$response = $request->send(); // $response should be an HttpMessage object
The manual for HttpRequest::setPostFiles states that the single argument of this method is an array of files to post. This is unclear and may mean an array of local file names, an array of file handles or an array of file contents. It shouldn't take long to figure out which is correct!
Here's an example that uses streams and does not rely on PECL.
// Simulate server side
if (isset($_GET['req'])) {
echo htmlspecialchars($_POST['data']);
exit();
}
/**
* Found at: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl
*/
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array('method' => 'POST',
'content' => $data));
if ($optional_headers !== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = #stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
// Example taken from: http://en.wikipedia.org/wiki/XML
// (Of course, this should be filled with content from an external file using
// file_get_contents() or something)
$xml_data = <<<EOF
<?xml version="1.0" encoding='ISO-8859-1'?>
<painting>
<img src="madonna.jpg" alt='Foligno Madonna, by Raphael'/>
<caption>This is Raphael's "Foligno" Madonna, painted
in <date>1511</date>-<date>1512</date>.</caption>
</painting>
EOF;
// Request is sent to self (same file) to keep all data
// for the example in one file
$ret = do_post_request(
'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?req',
'data=' . urlencode($xml_data));
echo $ret;

How can I POST a file to a REST server without writing the file to disk with PHP?

I'm trying to send an XML file to a server as part of the POST method for an internal API.
All the PHP documentation points to using the $postVars['file']='#/path/to/file.xml' to actually send the file.
I want to send the file from a string, but it still needs to be sent as a file upload, not a string.
Help?
Take a look at this thread it deals with what you want to do I think: http://www.webmasterworld.com/php/3164561.htm
The last entry might be of help (reformatted by me):
function do_post_request($url, $data, $optional_headers = null) {
$params = array('http' => array(
'method' => 'post',
'content' => $data
));
if ($optional_headers!== null)
$params['http']['header'] = $optional_headers;
$ctx = stream_context_create($params);
$fp = #fopen($url, 'rb', false, $ctx);
if (!$fp)
throw new Exception("Problem with $url, $php_errormsg");
$response = #stream_get_contents($fp);
if ($response === false)
throw new Exception("Problem reading data from $url, $php_errormsg");
return $response;
}
Basically the solution is to make use of the built-in php stream handling for urls.
http://www.sematopia.com/?p=153 and http://www.daniweb.com/forums/thread150772.html might help.

Categories