2010-10-13 29 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