How to HTTP GET while sending vars and retrieve XML output using PHP
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开发者_如何学C 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 doneallow_url_fopen is On
If xml is coming back, your browser will "hide" it - view the page source, and you will see it.
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).
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&password=password&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 & 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?
精彩评论