如何在发送变量时进行 HTTP GET 并使用 PHP 检索 XML 输出
我正在尝试使用此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 xml 返回,您的浏览器将“隐藏”它 - 查看页面源代码,您就会看到它。
If xml is coming back, your browser will "hide" it - view the page source, and you will see it.
这对我来说适用于
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 havefopen_wrappers
enabled in order for this to work. Do aphpinfo
and look forallow_url_fopen
.Another option would be to use the
cURL
library (which I would recommend).我尝试过,与我的代码配合得很好:
返回:
如果启用了allow_url_fopen,也许检查您的PHP配置?
I tried it, works just fine with my code:
returns:
Maybe check your PHP Configuration if allow_url_fopen is enabled?