Curl error when try to download file using Dropbox API - php

This is my first attempt using Dropbox API and I'm just following the "Hello World" from PHP SDK. Upload methods is working fine, but download:
$app = new DropboxApp("...", "...", "...");
$dropbox = new Dropbox($app);
$file = $dropbox->download("/image.png", '/image.png');
Return this warning:
Warning: curl_exec(): Could not call the CURLOPT_WRITEFUNCTION in
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php
on line 40
Fatal error: Uncaught RuntimeException: Unable to open /image.png
using mode w+: fopen(/image.png): failed to open stream: Permission
denied in
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/functions.php:299
Stack trace: #0 [internal function]: GuzzleHttp\Psr7{closure}(2,
'fopen(/unifeal-...', '/srv/www/website/...', 307, Array) #1
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/functions.php(307):
fopen('/image.p...', 'w+') #2
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/LazyOpenStream.php(37):
GuzzleHttp\Psr7\try_fopen('/image.p...', 'w+') #3
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/StreamDecoratorTrait.php(31): GuzzleHttp\Psr7\LazyOpenStream->createStream() #4
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/StreamDecoratorTrait.php(136):
GuzzleHttp\Psr7\LazyOpenStream->__get('stream') #5
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php(380):
GuzzleHttp\Psr7\LazyOpenStream-> in
/srv/www/website/public_html/dropbox/vendor/guzzlehttp/psr7/src/functions.php
on line 299
I'm testing using Vagrant.
UPDATE
I can confirm that is not a permission issue because download works using this script:
$file = $dropbox->download("/image.png");
$contents = $file->getContents();
file_put_contents(__DIR__ . "/image.png", $contents);
$metadata = $file->getMetadata();
But not using the recommended way.

Related

php foreach loop with urls from file

I am trying to the status of urls which are stored in a text file
in my code i have:
$filestatus = file("urlsmartins_status.txt");
foreach ($filestatus as $filestate){
$filestatusurl = (file_get_contents("$filestate"));
$filestatusurlxml = new SimpleXMLElement ($filestatusurl);
print_r ($filestatusurlxml);
}
in the urlsmartin_status.txt i have 5 urls written
http://172.27.73.5:8080/api/service/
http://172.27.73.6:8080/api/service/
http://172.27.73.7:8080/api/service/
http://172.27.73.8:8080/api/service/
But when i execute the code i have the following result
PHP Warning: file_get_contents(http://172.27.73.5:8080/api/service/
): failed to open stream: HTTP request failed! HTTP/1.1 404 OK
PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in ...
Stack trace:
#0 /opt/rh/httpd24/root/var/www/html/pub/martin.php(56): SimpleXMLElement->__construct('')
#1 {main}
thrown in /opt/rh/httpd24/root/var/www/html/pub/martin.php on line 56
What am i missing here?
Thanks
the problem here is that the lines in your file are not "XML", hence SimpleXMLElement cannot be instantiated.
I get the same error like this:
I managed to fix it via
$filedata = file_get_contents('urlsmartins_status.txt');
$urls = explode("\n", trim($filedata));
foreach($urls as $key => $url) {
$filestatusurl = file_get_contents("$url");
$filestatusurlxml = new SimpleXMLElement ($filestatusurl);
print_r ($filestatusurlxml);

how to fix Issue with codeigniter 4 after pushing to live server

After building my website on localhost it works fine but when I launched it to a live server, the homepage displays, and few other pages as others couldn't. Please can someone help me out?
I got this error
rewind($this->fileHandle);
207 }
208
209 $session_data = '';
210 clearstatcache(); // Address https://github.com/codeigniter4/CodeIgniter4/issues/2056
211 for ($read = 0, $length = filesize($this->filePath . $sessionID); $read < $length; $read += strlen($buffer))
212 {
213 if (($buffer = fread($this->fileHandle, $length - $read)) === false)
214 {
215 break;
216 }
217
218 $session_data .= $buffer;
219 }
and this
Fatal error: Uncaught ErrorException: Unknown: Cannot call session save handler in a recursive manner in Unknown:0 Stack trace: #0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler(2, 'Unknown: Cannot...', 'Unknown', 0, NULL) #1 {main} thrown in Unknown on line 0

how to check if simpleXML is null

So, I build a script which contains multi curl to verify the XML and extrac $xml->loc from more urls but my script crash(stop) when he found anything else which is not a xml file
...multi curl code...
if($httpcode>=200 && $httpcode<=300) {
$xml = new SimpleXMLElement($data);
if($xml !== NULL) {
foreach ($xml->url as $url_list) {
$url = $url_list->loc;
$newurls[] = $url;
}
}
}
If i put 5 valid xml urls it works but if I put 100 link and one of them is not a XML file the script stops.
ERROR:
[root#test ~]# php a z2
PHP Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /root/a:160
Stack trace:
#0 /root/a(160): SimpleXMLElement->__construct('\r\n<!DOCTYPE htm...')
#1 /root/a(65): mycurl_done('http://southpas...', '\r\n<!DOCTYPE htm...', Resource id #36)
#2 /root/a(175): ThreadsLoop()
#3 {main}
thrown in /root/a on line 160
[root#test ~]#
Seems like my script try to extract information from a html file because if the file don't exist it will redirect me to homepage(I guess)...
What options do I have to make to continue if the SimpleHTMLElement can't parse my $data variable which is the entire source code from a XML file?
You can use a try catch block to handle the exception.
try {
$xml = new SimpleXMLElement($data);
//XML is valid
...
} catch(Exception $e) {
//XML is invalid
}

Dropbox API for PHP gives error

I am trying to upload a file in chunks via Dropbox's API for PHP.
This is my code: This gives out the folowing error:
First few chunks are uploaded and then I get this error:-
Fatal error: Uncaught exception 'Dropbox\Exception_NetworkIO' with message 'Erro
r executing HTTP request: Failed to connect to api-content.dropbox.com port 443:
Connection refused' in /home/yashtk1/public_html/dropbox/Dropbox/Curl.php:69
Stack trace:
#0 /home/yashtk1/public_html/dropbox/Dropbox/Client.php(721): Dropbox\Curl->exec
()
#1 /home/yashtk1/public_html/dropbox/Dropbox/Client.php(602): Dropbox\Client->_c
hunkedUpload(Array, '`S\xCDb0\x80`\xA0\xD3\xB7\xEA\x07\xF1\xF7\xB4...')
#2 /home/yashtk1/public_html/dropbox/Dropbox/Client.php(437): Dropbox\Client->ch
unkedUploadContinue('jB2ZQWVyzMQgsX4...', 41943040, '`S\xCDb0\x80`\xA0\xD3\xB7\x
EA\x07\xF1\xF7\xB4...')
#3 /home/yashtk1/public_html/dropbox/Dropbox/RequestUtil.php(278): Dropbox\Clien
t->Dropbox\{closure}()
#4 /home/yashtk1/public_html/dropbox/Dropbox/Client.php(438): Dropbox\RequestUti
l::runWithRetry(3, Object(Closure))
#5 /home/yashtk1/public_html/dropbox/Dropbox/Client.php(381): Dropbox\Client->_u
ploadFileChunked('/ahmd1.zip', Object(Dropbox\WriteMode), Resource i in /home/ya
shtk1/public_html/dropbox/Dropbox/Curl.php on line 69
My script:-
<?php
echo "hi";
set_time_limit(0);
# Include the Dropbox SDK libraries
require_once "Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("config");
$f = fopen("ahmd1.zip", "rb");
$dbxClient = new dbx\Client("Access code", "PHP-Example/1.0");
$mode = dbx\WriteMode::add();
$chunk = $dbxClient->uploadFileChunked("/ahmd1.zip", $mode, $f);
print_r($chunk);
//uploadFileChunked( string $path, Dropbox\WriteMode $writeMode, resource $inStream, integer|null $numBytes = null, integer|null $chunkSize = null );
fclose($f);
?>
Please can anyone point out the mistake ?

YouTube PHP API - Zend_Gdata_App_HttpException

I've got a working website, which uses youtube api. Everything was fine but today I noticed I get error while using this api:
Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 403 <?xml version='1.0' encoding='UTF-8'?><errors><error><domain>yt:quota</domain><code>too_many_recent_calls</code></error></errors>' in /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata/App.php:700 Stack trace: #0 /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata.php(221): Zend_Gdata_App->performHttpRequest('GET', 'http://gdata.yo...', Array, NULL, NULL, NULL) #1 /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata/App.php(861): Zend_Gdata->performHttpRequest('GET', 'http://gdata.yo...', Array) #2 /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata/App.php(754): Zend_Gdata_App->get('http://gdata.yo...', NULL) #3 /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata/App.php(205): Zend_Gdata_App->importUrl('http://gdata.yo...', 'Zend_Gdata_YouT...', NULL) #4 /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata.php( in /root/public_html/grabandrun.com/public/ZendGdata/library/Zend/Gdata/App.php on line 700
my code looks somehow like this:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_YouTube');
Zend_Loader::loadClass('Zend_Uri_Http');
$yt = new Zend_Gdata_YouTube();
$query = $yt->newVideoQuery();
$query->setQuery($searchTerms);
$query->setStartIndex($index2);
$query->setMaxResults(10);
$query->setOrderBy($order);
$videoFeed = $yt->getVideoFeed($query);
foreach ($videoFeed as $videoEntry) {
$videoThumbnails = $videoEntry->mediaGroup->thumbnail[0]->url;
$title = $videoEntry->mediaGroup->title;
$vidId = $videoEntry->getVideoId();
$vidDescription = $videoEntry->mediaGroup->description;
$duration = $videoEntry->mediaGroup->duration->seconds;
$rating = round($videoEntry->rating->average,1)." out of 5";
echo "info...";
}
Funny thing is that it sometime works and sometime does not... I don't really know why, any help appreciated, thanks.
As stated in comments, I just had to limit the access and lower the calls. Thanks Slava.

Categories