Calling outside programs with $_GET
-
Here is the thing. I have created a site and on it we need to call a program that downloads vcards, one for each employee. That means I need to pass the id for that employee. I am trying $_GET. I have tried a straight call passing the parm. I tried creating a page and passing the parm thru the page. Here is a link: http://hsm.bothwip2013.com/attorneys/. I have never attempted this before. Any ideas?
Two samples of code attempted:<a href=\"http://hsm.bothwip2013.com/atty-vcard/?id=$attorney_id\" class=\"atty-vcard\" title=\"Download Vcard\"> <a href=\""; ?> <?php bloginfo('template_url'); ?> <? echo "child/attorney-vcard.php?id=$attorney_id\" class=\"atty-vcard\" title=\"Download Vcard\">
Thanks
-
Outputting a link would require user interaction to click on the link. The response would then be sent to the user’s browser, not your script.
You will want to use
file_get_contents()
or cURL to make external requests where your code can do something with the response. The examples you will see are typically for accessing remote data from a different server. They should work equally well for resources on the same server but in a separate name space.I am searching the documentation and can’t find anything on how to use either of your suggestions. Do you have any examples?
A quick google search brought this up…
http://us2.php.net/function.file-get-contentsHas working examples and everything….
Thanks Evan. I am trying to see how I can accomplish my original goal with this functionality. I want to be able to call a php program that creates a vcard file or prints the attorney bio based on the ID passed using $_GET.
Sorry to seem dense I am just not seeing how to do this from the examples.
The closest thing I have found to a solution is this link on stack overflow: output-buffering-with-php-headers
He is doing the same thing I am trying to accomplish. When I implement his code I get this error: “BEGIN:VCARD VERSION:3.0 N:Stone;Kenneth;R.;;; FN:Kenneth R. Stone TITLE:Partner PHOTO;VALUE=URL;TYPE=JPG:http://www.baileyglasser.com/images/attorney_photos/kstone.png ORG:Hefner, Stark & Marois, LLP TEL;WORK;VOICE: TEL;WORK;FAX: EMAIL;TYPE=PREF;INTERNET:kstone@hsmlaw.com ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;2150 River Plaza Drive=0D=0ASuite 450;Sacramento;CA;95833;United States; URL;TYPE=WORK:http://www.baileyglasser.com END:VCARD Warning: Cannot modify header information – headers already sent by (output started at /var/www/hsm.bothwip2013.com/wp-includes/link-template.php:1640) in /var/www/hsm.bothwip2013.com/wp-content/themes/Featherchild/attorney-vcard.php on line 59 Warning: Cannot modify header information – headers already sent by (output started at /var/www/hsm.bothwip2013.com/wp-includes/link-template.php:1640) in /var/www/hsm.bothwip2013.com/wp-content/themes/Featherchild/attorney-vcard.php on line 60 “
Here is my code:
<?php ob_start(); require "code/dbconn.php"; if ($attorney_id) { $query="SELECT a.*, l.* FROM attorneys AS a JOIN lawfirm AS l ON l.id = a.attorney_office WHERE attorney_id = $attorney_id"; $result=mysql_query($query); $myrow = mysql_fetch_array($result); $seoname=$myrow["seo_title"]; $first=$myrow["attorney_fname"]; $middle=$myrow["attorney_mname"]; $last=$myrow["attorney_lname"]; $attorney_email=$myrow["attorney_email"]; $attorney_office=$myrow["attorney_office"]; $tel=preg_replace ('/^(\d+)\.(\d+)\.(\d+)$/', '($1) $2-$3', $myrow["attorney_tel"]); $fax=preg_replace ('/^(\d+)\.(\d+)\.(\d+)$/', '($1) $2-$3', $myrow["attorney_fax"]); $attorney_title=$myrow["attorney_title"]; if ($middle) { $fullname = (isset ($middle)) ? "$first $middle $last" : "$first $last"; } if (!$middle) { $fullname = (isset ($middle)) ? "$first $last" : "$first $last"; } $filename = preg_replace ('/ +/', '_', $fullname); $filename = preg_replace ('/[^a-z_]+/i', '', $filename); $photo = $myrow["attorney_photo"]; $path = "http://www.baileyglasser.com/images/attorney_photos/"; $office = $myrow["office"]; $address =$myrow["address"]; $address2 =$myrow["address2"]; $city =$myrow["city"]; $state =$myrow["state"]; $zip =$myrow["zip"]; $country ="United States"; $query1 = "SELECT * FROM titles WHERE $attorney_title=title_id "; $result1 = mysql_query($query1); $myrow1 = mysql_fetch_array($result1); $title = $myrow1["title_name"]; print <<<END BEGIN:VCARD VERSION:3.0 N:$last;$first;$middle;;; FN:$fullname TITLE:$title PHOTO;VALUE=URL;TYPE=JPG:$path$photo ORG:$office TEL;WORK;VOICE:$tel TEL;WORK;FAX:$fax EMAIL;TYPE=PREF;INTERNET:$attorney_email ADR;WORK;ENCODING=QUOTED-PRINTABLE:;;$address=0D=0A$address2;$city;$state;$zip;$country; URL;TYPE=WORK:http://www.baileyglasser.com END:VCARD END; $output = ob_get_contents(); header('Content-Type: text/x-vcard'); header('Content-Disposition: attachment; filename=vCard_' . $filename . '.vcf'); return $output; exit(); } ?>
Thanks for any further assistance.
Sorry for the slow response, I’ve been away. I completely misconstrued what you are trying to do. Please allow me another try 🙂
The headers already sent error is because you used
print
before you usedheader()
. Callingheader()
before usingprint
will resolve this, but you still need to address some other issues.First and foremost you must include the ID value from the GET URL parameter when you construct the query. Let’s say your GET request looks like this:
www.example.com/scripts/my-php-script.php?id=123
Your script could then assign the value passed in ‘id’ (123) to
$attorney_id
like so:
$attorney_id = $_GET['id'];
Assuming all of your mySQL script is correct, then correct values are returned based on the ID of 123. You can drop the output buffer stuff (functions beginning with
ob_
), it has no bearing on what you are trying to do here.The first thing sent to the browser are your
header()
calls. After headers are sent you can go ahead andprint
the v-card data. There is no need toreturn()
anything, you are not declaring a function to return from. Theexit()
is OK, but the script will end anyway as there is no code left to execute.For my example URL to work, all of your code should reside in a PHP file named my-php-script.php that resides in the scripts folder of your server. If you use different names, alter the URL accordingly.
Thanks bcworkz, but the solution to the first part of the problem was to condition the get_header that if calling the vcard.php don’t do get_header. Now I get the vcard to download. The problem is now that I get two lines of blank spaces before the begin:vcard so when I try to import in contact it will not recognize as a vcard. I tried using ‘echo trim($output);’ and ‘return trim($output);’ on the output but no luck. While trim is supposed to remove the leading blanks, it is not working in this instance. Any suggestions?
Thanks
MichaelWell, it’s progress at least 🙂
Extraneous white space can be a little tricky to track down. Using
trim()
is a good thing to do, but obviously not enough. Be sure there is no whitespace before the file’s first<?php
, not even the byte order mark that often creeps into UTF-8 files. You may need to view the file in a hex editor to confirm this. Not directly related, but you do not need the closing?>
at the end, deleting this prevents stray line feeds and whitespace from being output after the v-card content.Check for similar issues in included files. The only other possibility is maybe I was wrong about the output buffer stuff and there is already something else going on with it. By managing the v-card portion in a separate buffer, you can prevent other buffer content from interfering with the v-card output.
OK so we refer back to the code above, are you referring to the beginning <?php because there are no spaces there. Maybe I should be writing the file another way? Not sure. Why I use this same code in a non-Wordpress site it works like a charm. There is something in WordPress that causes the spaces to happen. Still searching for the solution.
Thanks for any further assistance.
Here is the final solution: I found a plugin, hcard-vcard-generator-wordpress-plugin. I modified that to accept my files data and it worked fine.
Thanks for all the help.
Just as a note, the last two lines of your file should be:
echo $output; exit();
Saying
return
works sort of the same way, but is meant to be part of a function, not a bit of procedural code like this. There’s a chance that could be where the extra spaces are creeping in.
- The topic ‘Calling outside programs with $_GET’ is closed to new replies.