2010-10-13 27 views
-1

我想使用这个API检索XML输出:www.cpsc.gov/cpscpub/prerel/api.html如何HTTP GET而发送VAR和使用PHP

文档:www.cpsc.gov/ cpscpub/prerel/requirements.pdf

这里是位置的呼叫要被发送,其中还包括示例代码段:http://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx

的getRecallByWord函数应返回的XML数据。

下面是用于获取数据的预制URL(注意,已经根据文档使用https): www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc.asmx/getRecallByWord?message1=3M &密码=密码&用户id =用户id

在本文档中有没有具体的用户名和密码是必需的(任何将工作)

我试过的fopen,的file_get_contents和HTTP_GET(虽然最后一个没有因为工作的说明扩展未安装)。

$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"; 

输出:
资源ID#3done做

了allow_url_fopen是在

+1

”什么都不会工作“不是一个能帮助你的问题描述。 – 2010-10-13 10:05:06

+0

是的。请显示一些代码和你得到的错误/问题 – 2010-10-13 10:08:17

+1

“不想工作”也不会帮助你。必须有**描述**你得到了什么。必须有**详细的**解释你做了什么(不只是提及函数名称,而是**实际**代码),你得到了什么。 – 2010-10-13 10:10:11

回答

0

这对我的作品与file_get_contents(不要忘记网址前加协议)。 PHP手册指定您需要启用fopen_wrappers才能使其工作。做一个phpinfo并寻找allow_url_fopen

另一种选择是使用cURL库(我会推荐)。

+0

协议部分应该是什么样子? – Aaron 2010-10-13 10:22:31

+0

URL前面的“https://”。 – 2010-10-13 10:24:24

+0

感谢您的帮助。 – Aaron 2010-10-13 10:30:29

0

我试了一下,工作得很好我的代码:

<?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(); 

回报:

<?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> 

也许检查你的PHP配置如果allow_url_fopen已被激活?

+0

allow_url_fopen启用 – Aaron 2010-10-13 10:18:26

+0

2事情,如果你想使用fopen你不能这样访问它,你的代码将看起来像:'$ handle = fopen(“https://www.cpsc.gov/cgibin/CPSCUpcWS/CPSCUpcSvc的.asmx/getRecallByWord MESSAGE1 = 3M&密码=密码&用户ID =用户id”,R)?; (!)feof($ handle)){$ buffer = fgets($ handle,4096); echo $ buffer; } fclose($ handle); print“done”;'2.你的例子(除了fopen的小错误)工作得很好 - 对不起 – Hannes 2010-10-13 10:22:48

+0

感谢您的帮助。 – Aaron 2010-10-13 10:30:52

2

如果xml返回,您的浏览器将“隐藏”它 - 查看页面源代码,您将看到它。 “

+0

我现在看到它,谢谢。 – Aaron 2010-10-13 10:27:59