Sorry, this might be pretty basic. I'm trying to use the Yelp API and am running a test search for McDonalds in Baltimore.
this is the code:
<?php
$AccountKey = "XXXX";
$restaurant = "McDonalds";
$city = "Baltimore";
$file = "test.txt";
$data = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
file_put_contents($file, $data);
?>
I'm trying to store the results in test.txt which I can then parse but its not working. Any ideas?
Thanks in advance!
In your code you didn't open the text file.
// Open the file to get existing content
$data = file_get_contents($file);
$data. = http_get("http://api.yelp.com/business_review_search?term=".$restaurant."&location=".$city."&ywsid=".$AccountKey);
// Write the contents back to the file
file_put_contents($file, $data);
more details
Related
i am trying to save a mp3 file from a website "http...../filename.mp3" to my server from with the following php code, i receive the url in the $url from ajax. thats my code:
$url = $_POST['data'];
$filename = substr($url, strrpos($url, '/') + 1);
$directory = './fileuploads/tmp/';
$data = file_get_contents($url);
$file = fopen($directory.$filename, "w+");
fwrite($file, $data);
fclose($file);
It works fine but the new mp3 file on my server is "empty". it doesnt play any sound. how do you guys would solve such a problem? please make it as simple as possible. best regards
I'm trying to obtain data from my Adobe Media server. For instance when I navigate with my browser to this URL:
http://misite.com:1111/admin/getLiveStreamStats?auser=myuname&apswd=mypwd&appInst=live&stream=srd
misite.com should be localhost. I get the contents shown in my browser. Now I'm trying to get those contents inside my php file:
$url = 'http://misite.com:1111/admin/ping?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
//OR:
print($contents)
But this gives me only a blank page. I've checked my source code and it returns empty. What should I do?
Any suggestions?
$url = 'http://misite.com:1111/admin/ping?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
This has no file to refer to. Notice ping is not ping.php
$url = 'http://example.com/somephpscript.php?auser=myuname&apswd=mypwd';
$contents = file_get_contents($url);
echo $contents;
Trying to fwrite the pdf results from PrinceXML to the server (in a new pdf file) instead of exporting via headers to acrobat. The below code displays the results on the browser.
require_once("../library/Prince/prince.php");
$princeSettings = $this->getInvokeArg('bootstrap') >getOption('prince');
$prince = new Prince($princeSettings['path']);
$prince->setHTML(true);
$result = $prince->convert_string_to_passthru($this->htmlView);
$fp = fopen("./files/reports/report.pdf", "w");
fwrite($fp, $result);
fclose($fp);
Figured this out for anyone who want to know...
require_once("../library/Prince/prince.php");
$princeSettings =$this->getInvokeArg('bootstrap')->getOption('prince');
$prince = new Prince($princeSettings['path']); $prince->setHTML(true);
$pdfPath =realpath(APPLICATION_PATH . "/../public/files/reports/report.pdf");
$prince->convert_string_to_file($this->htmlView, $pdfPath);
I'm writing an application that uses a .php script to get tweets using the twitter search API.
See below code:
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
// Local path
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag. "%20-RT") or die("Could not get tweets");
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
?>
My problem is that I want to make sure that this script runs without fail, or if it does fail at least doesn't keep looping.
The script is going to be run automatically every 1 minute.
Would anyone know a good way to handle errors here?
TL;DR: How do I handle errors on my code?
in simple, use '#' prefix for function. It suppresses errors from displaying. Read More Here
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = #file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag . "%20-RT");
if (!empty($json)) {
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
} else {
echo "Could not get tweets";
exit;
}
?>
I needed to generate an XML file through Wordpress to be used for generating markers on a Google Map.
I modified a function I found here: Write to XML file using fopen in Wordpress
The function runs whenever a post is modified.
The map side of things is working fine. I can generate an XML file which has a title and longitude and latitude for each entry and they are properly plotted on the map.
However I can't seem to get the post content, which I want to use for the address. I can't even get the content just to echo for testing. I've tried encoding the html in case that was conflicting with the XMl, but nothing. Seems to just not be getting any content. Yes I have made sure the posts have content although I am open to the idea I may have missed something simple :-p
My function is below.
add_action( 'save_post', 'markers_xml' );
function markers_xml(){
if ($_POST['post_type'] == 'places-to-eat')
{
$xml = new SimpleXMLElement('<xml/>');
$markers = get_posts( array( 'post_type'=>'places-to-eat', 'numberposts'=>-1 ) );
$xml->addChild('markers');
foreach($markers as $i=>$marker){
$name = get_the_title($marker->ID);
$address = get_the_content($marker->ID);
$address = htmlentities($address);
$lat = get_post_meta($marker->ID, 'the-lat', true);
$lng = get_post_meta($marker->ID, 'the-lng', true);
$xml->markers->addChild('marker');
$xml->markers->marker[$i]->addChild('name', $name);
$xml->markers->marker[$i]->addChild('address', $address);
$xml->markers->marker[$i]->addChild('lat', $lat);
$xml->markers->marker[$i]->addChild('lng', $lng);
}
$file = '/public_html/wp-content/uploads/test.xml';
$open = fopen($file, 'w') or die ("File cannot be opened.");
fwrite($open, $xml->asXML());
fclose($open);
}
}
The title and content are already available to you through the get_posts(). There is no need to use get_the_title() or get_the_content(). The return values for the get_posts() is stated in the get_post() documentation. This shows you the data available.
Try this.
foreach($markers as $i=>$marker){
//replace this
//$name = get_the_title($marker->ID);
//with this
$name = $marker->post_title;
//replace this
//$address = get_the_content($marker->ID);
//with this
$address = $marker->post_content;