[英]Attempting to parse XML/JSON from an API output
OK... I am using PHP 5 (be gentle, still learning PHP). 好的......我正在使用PHP 5(温柔,还在学习PHP)。 CURL is enabled.
CURL已启用。 Attempting to load XML or JSON output from an API to an object and nothing happens.
尝试将API或API中的XML或JSON输出加载到对象,但没有任何反应。 When I manually execute the URL in question, I get what I am expecting.
当我手动执行有问题的URL时,我得到了我期待的东西。
Here is my code: 这是我的代码:
class XmlToJson {
public function Parse ($url) {
$fileContents = file_get_contents($url);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
return $json;
}
}
$_MySQLServer = "localhost";
$_MySQLServerUserName = "";
$_MySQLServerPassword = "";
$_MySQLDatabaseName = "";
$_SSActiveWear_UserID = "*****";
$_SSActiveWear_APIKey = "*****";
$_SSActiveWear_APIBaseURL = "https://*****/v2";
$_CategoryURL = "/categories/";
$_StylesURL = "/styles/";
$_ProductsURL = "/products/";
$_SpecsURL = "/specs/";
$_SSActiveWear_MediaType = "xml";
//$_conn = mysqli_connect($_MySQLServer, $_MySQLServerUserName, $_MySQLServerPassword, $_MySQLDatabaseName);
//Insert or Update Categories
$_URL = $_SSActiveWear_APIBaseURL . $_CategoryURL;
$_URL = $_URL . "?mediatype=$_SSActiveWear_MediaType&UserName=$_SSActiveWear_UserID&Password=$_SSActiveWear_APIKey";
$OBJ = simplexml_load_string($_URL);
print_r($OBJ);
What am I doing wrong? 我究竟做错了什么?
Edit 1 编辑1
Added the following code: 添加了以下代码:
$xml = simplexml_load_file($_URL) or die("Error: Cannot create object");
print_r($xml);
and it dies. 它死了 Does that mean that there is something wrong with the code?
这是否意味着代码有问题?
Try this : 尝试这个 :
$OBJ = simplexml_load_string(file_get_contents($_URL));
If you want to know why your code is not working, you are trying to load XML from URL but "simplexml_load_string" loads XML from string. 如果您想知道代码无效的原因,您尝试从URL加载XML,但“simplexml_load_string”从字符串加载XML。
I FINALLY figured it out... More to the point I finally found a site on Google that helped. 我终于想通了......更重要的是,我终于在Google上找到了一个有帮助的网站。 It is the first answer in fsockopen with http authentication problem .
这是fsockopen中第一个带有http身份验证问题的答案。
So here is the code that works: 所以这是有效的代码:
file_get_contents("https://$_SSActiveWear_UserID:$_SSActiveWear_APIKey@$_SSActiveWear_APIBaseURL$_CategoryURL/?mediatype=$_SSActiveWear_MediaType");
mediatype can be either json or xml mediatype可以是json或xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.