views:

39

answers:

3

I'm trying to use this API: www.cpsc.gov/cpscpub/prerel/api.html

Documentation: www.cpsc.gov/cpscpub/prerel/requirements.pdf

Here is the location calls are to be sent, which also includes sample code snippets: http://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx

The getRecallByWord function should return XML data.

Here's a preformed URL for getting the data (note, have to use https according to doc): www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3M&password=password&userId=userId

In the documentation there is a note that no specific username or password is required (anything will work)

I've tried fopen, file_get_contents, and http_get (although the last one didn't work since extension isn't installed).

$result = fopen("https://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3M&password=password&userID=userId",r);
print $result;
print "done";
$response = file_get_contents("https://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3M&password=password&userID=userId");
print $response;
print "done";

Output:
Resource id #3done done

allow_url_fopen is On

A: 

Hi,

This works for me with file_get_contents (don't forget to add the protocol before the URL). The PHP manual specifies that you need to have fopen_wrappers enabled in order for this to work. Do a phpinfo and look for allow_url_fopen.

Another option would be to use the cURL library (which I would recommend).

Alin Purcaru
what should the protocol portion look like?
Aaron
The `https://` before the URL.
Alin Purcaru
thanks for the help.
Aaron
A: 

I tried it, works just fine with my code:

<?php 
    $content = file_get_contents('http://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3M&amp;password=password&amp;userId=userId');
    header('Content-Type: text/xml; charset=utf-8');
    print $content;
    exit();

returns:

<?xml version="1.0" encoding="utf-8"?>
<message outcome="success" transactionID="6B3E350A-C90B-4726-A237-F76FC51C4237">
  <results>
    <result UPC="" recallNo="73017" recallURL="http://www.cpsc.gov/cpscpub/prerel/prhtml73/73017.html" recDate="" y2k="73017" manufacturer="3M" type="" prname="Shipping Mate Palletizing Adhesive aerosol spray adhesives" hazard="" country_mfg="" />
    <result UPC="" recallNo="96097" recallURL="http://www.cpsc.gov/cpscpub/prerel/prhtml96/96097.html" recDate="1996-04-21" y2k="96097" manufacturer="3M" type="Projectors" prname="3M overhead projectors" hazard="Electrocution/Electric Shock" country_mfg="" />
    <result UPC="" recallNo="73014" recallURL="http://www.cpsc.gov/cpscpub/prerel/prhtml73/73014.html" recDate="" y2k="73014" manufacturer="3M" type="Arts &amp; Crafts" prname="Foil Art Spray Adhesive aerosol spray adhesives" hazard="" country_mfg="" />
  </results>
</message>

Maybe check your PHP Configuration if allow_url_fopen is enabled?

Hannes
allow_url_fopen is enabled
Aaron
2 Things, if you wanna use fopen you cant access it that way, your code would have to look like: `$handle = fopen("https://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3Mwhile (!feof($handle)) { $buffer = fgets($handle, 4096); echo $buffer;}fclose ($handle);print "done";` 2. Your example (besides the small mistake with fopen) works just fine - Sorry
Hannes
thanks for the help.
Aaron
+2  A: 

If xml is coming back, your browser will "hide" it - view the page source, and you will see it.

cweiske
I see it now, thanks.
Aaron
then please accept the answer
cweiske