如何在发送变量时进行 HTTP GET 并使用 PHP 检索 XML 输出

发布于 2024-09-27 09:57:24 字数 1050 浏览 4 评论 0原文

我正在尝试使用此 API:www.cpsc.gov/cpscpub/prerel/api.html

文档: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&password=password&userId=userId

在文档中有一条注释,不需要特定的用户名或密码(任何都可以)

我已经尝试了 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 已完成

allowed_url_fopen 已开启

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱我好吗 2024-10-04 09:57:24

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

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

悲欢浪云 2024-10-04 09:57:24

这对我来说适用于 file_get_contents (不要忘记在 URL 之前添加协议)。 PHP 手册指定您需要启用 fopen_wrappers 才能使其正常工作。执行 phpinfo 并查找 allow_url_fopen

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

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).

违心° 2024-10-04 09:57:24

我尝试过,与我的代码配合得很好:

<?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 & Crafts" prname="Foil Art Spray Adhesive aerosol spray adhesives" hazard="" country_mfg="" />
  </results>
</message>

如果启用了allow_url_fopen,也许检查您的PHP配置?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文