I have link in specific variable eg.
$link = 'http://google.com'
and I try to get content from this link with function fopen.
Eg. : $var = fopen("'".$link."'", "rb");
echo stream_get_contents($var); ,
but without success. Error is
Warning: file_get_contents('http://google.com'): failed to open stream: No such file or directory in /var/www/...
If I use directly
$var = fopen('http://google.com', "rb");
echo stream_get_contents($var)
this work perfectly?
How do I fix this or what method to use if I link is a variable?
Based on your posted code, this worked for me. Try it using this method:
<?php
$link = "http://www.google.com";
$var = fopen($link, "rb");
echo stream_get_contents($var)
?>
This always worked for me.
$url = 'http://google.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
Related
As mentioned earlier , i am working on the local server as of now in xampp. I have created 2 files index.php and test.php. What i want to achieve is that , index.php will send json data to test.php , with the json data received , the test.php is able to use that json data to turn the statistics into graph.
I am working with the first step , but however , nothing seems to display on my test.php when i tried to do a var_dump($data) , and what i get is NULL. Tried alot of solutions online but none to seems to fix it. I am relatively new to this , so really thanks and appreciate of your help. First php is index.php , second php is test.php.
Do i require a live server as of now in order to see the results in test.php or local server unable to display the result?
<?php
$array = array();
$product = array();
$product[0]['id_product'] = 'A01';
$product[0]['name_product'] = 'Sandal';
$product[0]['price_product'] = '500';
$product[1]['id_product'] = 'A02';
$product[1]['name_product'] = 'Shoes';
$product[1]['price_product'] = '2500';
$array['id'] = '123';
$array['note'] = 'this is my short example';
$array['data'] = $product;
$data = json_encode($array);
$ch = curl_init('http://localhost:8080/practice3/test.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, count($data));
$result = curl_exec($ch);
var_dump($result);
?>
```````````````````````````````````
<?php
$fp = fopen('php://input', 'r');
$raw = stream_get_contents($fp);
$data = json_decode($raw,true);
echo "hello";
echo $data['id'];
echo $data['note'];
foreach ($data['data'] as $key) {
echo 'id_product : '.$key['id_product'].'<br/>';
echo 'name_product : '.$key['name_product'].'<br/>';
echo 'price_product : '.$key['price_product'].'<br/>';
}
?>
````````````````````````````````````
I'm trying to get the redirect url from a stream using php.
Here's the code I have right now:
<?php
$stream = 'https://api.soundcloud.com/tracks/178525956/stream?client_id=XXXXXX';
$ch = curl_init($stream);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = curl_exec($ch);
echo $url;
curl_close($ch);
?>
Which outputs as a string:
{"status":"302 - Found","location":"THE_URL_I_WANT"}
So how ould I go about getting the url I want as a variable?
Thanks
It's simple use json_decode
$data = json_decode($url);
$your_url = $data->location;
How about
$data = json_decode($url);
$location = $data["location"];
I'm trying to run PHP script (from a Linux server) that will download a file through direct download link and save it on my server.
here is the script I'm using:
<?php
$url = 'http://download.maxmind.com/app/geoip_download?edition_id=108&date=20131015&suffix=zip&license_key=XXXXXXXXXXX';
$path = '/apps/test/';
$fp = fopen($path, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
for some reason it doesn't work for me, any suggestions ?
You need to verify that the ports are open on your firewall and use the below command:
(this will also download the file in the original format)
shell_exec("wget -P /apps/birst/php_test_scripts/ --content-disposition "."'"."https://download.maxmind.com/app/geoip_download?edition_id=108&suffix=zip&license_key=XXXXXXXX"."'");
Why dont you just use :
shell_exec("wget -P /target/directory/ http://download.link.com/download.zip");
Try this
$url = 'http://download.maxmind.com/app/geoip_download?edition_id=108&date=20131015&suffix=zip&license_key=XXXXXXXXXXX';
$path = '/apps/test/';
$filepath = $path .'file.zip';
$data = file_get_contents($url);
file_put_contents($filepath, $data);
I have a PHP file that's running a simple check for currency conversion. It works perfectly when I run it through my browser but my goal is to build a cron. When I run the script via SSH:
php /path/to/file.php
I get the following:
PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in /path/to/file.php on line 36
Where line 36 is:
fwrite($fh, json_encode($conversions));
...where $conversions is a simple single-dimension array
Here's the file:
$conversions = array();
$currencies = json_decode(file_get_contents("/path/to/currencies.json"), true);
foreach($currencies as $cur=>$data){
//make string to be put in API
$string = "1USD=?".$data['code'];
//Call Google API
$google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $google_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$result = curl_exec($ch);
//Explode result to convert into an array
$result = explode('"', $result);
$converted_amount = explode(' ', $result[3]);
$conversion = $converted_amount[0];
//$conversion = $conversion * $amount;
$conversions[$cur] = $conversion;
if($conversion==0){ exit('0 Return Error'); }
curl_close($ch);
}
$fh = fopen("/path/to/currency_conversions.json", 'w') or die("can't open file");
fwrite($fh, json_encode($conversions));
fclose($fh);
Well, I searched for you and I found the json_encode acceptes only UTF-8
so here is the solution:
for($i=0;$i<count($conversions);$i++)
$conversions[$i] = utf8_encode($conversions[$i]);
fwrite($fh, json_encode($conversions));
Try updating your PHP version to a version later than version 5.2.7
Older version have UTF-8 bugs.
I was getting youtube title and youtube description form the same code but now its not working
I am getting following error:
Warning: DOMDocument::load() [domdocument.load]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16
Warning: DOMDocument::load(http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY) [domdocument.load]: failed to open stream: no suitable wrapper could be found in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://gdata.youtube.com/feeds/api/videos/Y7G-tYRzwYY" in /home/colorsfo/public_html/zaroorat/admin/pages/addSongProcess.php on line 16
....................................
Following Coding is used to get Youtube Video Data:
$url = "http://gdata.youtube.com/feeds/api/videos/".$embedCodeParts2[0];
$doc = new DOMDocument;
#$doc->load($url);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;
It was working before (This coding is working fine in Local server but on internet its not working) but now its not working. Please guide me how to fix this error.
Thanks for your time.
Your server's allow_url_fopen is disabled (so is mine). I feel your pain. Here's what I did.
Try using cURL, but return your data in json, using YouTube's v2 api. You do that by appending that data to the end of your url.
?v=2&alt=json
You didn't post how you're getting your YouTube ID, and that may be a part of the issue (though your sample url did work). So just in case, I'm also posting a simple function to retrieve the ID from the YouTube video url.
function get_youtube_id($url) {
$newurl = parse_url($url);
return substr($newurl['query'],2);
}
OK, now assuming you have your video id, you can run the following function for each field you wish to return.
// Grab JSON and format it into PHP arrays from YouTube.
// Options defined in the switch. No option returns entire array
// Example of what the returned JSON will look like, pretty, here:
// http://gdata.youtube.com/feeds/api/videos/dQw4w9WgXcQ?v=2&alt=json&prettyprint=true
function get_youtube_info ( $vid, $info ) {
$youtube = "http://gdata.youtube.com/feeds/api/videos/$vid?v=2&alt=json";
$ch = curl_init($youtube);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
//If $assoc = true doesn't work, try:
//$output = json_decode($output, true);
$output = json_decode($output, $assoc = true);
//Add the ['feed'] in if it exists.
if ($output['feed']) {
$path = &$output['feed']['entry'];
} else {
$path = &$output['entry'];
}
//set up a switch to return various data bits to return.
switch($info) {
case 'title':
$output = $path['title']['$t'];
break;
case 'description':
$output = $path['media$group']['media$description']['$t'];
break;
case 'author':
$output = $path['author'][0]['name'];
break;
case 'author_uri':
$output = $path['author'][0]['uri'];
break;
case 'thumbnail_small':
$output = $path['media$group']['media$thumbnail'][0]['url'];
break;
case 'thumbnail_medium':
$output = $path['media$group']['media$thumbnail'][2]['url'];
break;
case 'thumbnail_large':
$output = $path['media$group']['media$thumbnail'][3]['url'];
break;
default:
return $output;
break;
}
return $output;
}
$url = "http://www.youtube.com/watch?v=oHg5SJYRHA0";
$id = get_youtube_id($url);
echo "<h3>" . get_youtube_info($id, 'title') . "</h3>"; //echoes the title
echo "<p><img style='float:left;margin-right: 5px;' src=" . get_youtube_info($id, 'thumbnail_small') . " />" . get_youtube_info($id, 'description') . "</p>"; //echoes the description
echo "<br style='clear:both;' /><pre>";
echo print_r(get_youtube_info($id));
echo "</pre>";
DOMDocuments' load() function uses PHPs fopen wrappers to retrieve files.
It seems that on your webserver, allow_url_fopen is set to 0, thus disabling these wrappers.
Try adding the following line to the top of your script:
ini_set ('allow_url_fopen', 1);
UPDATE: Try this:
<?php
$url = "http://gdata.youtube.com/feeds/api/videos/" . $embedCodeParts2[0];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$file = curl_exec($ch);
curl_close($ch);
$doc = new DOMDocument;
#$doc->loadHTML($file);
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue;
$videoDescription = $doc->getElementsByTagName("description")->item(0)->nodeValue;
I hope it is not too late. My solution is to edit /etc/resolv.conf in your Linux machine: and replace first line with below line:
nameserver 8.8.8.8
Then save the file. no need for service restart.
Might work for servers who disabled some function accidentally for security.