I am trying to use the DELL API to fetch warranty information from our products. DELL provides an XML file that I want to extract the end of warranty date from.
Location of the XML file: https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXX1&apikey=YYYYYYYYYYYYYYYYY
[Note: svctags and apikey obfuscated]
Code:
$xml = simplexml_load_file("https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=XXXXXXX1&apikey=YYYYYYYYYYYYYYYYYY");
print_r($xml->GetAssetWarrantyResult->{'a:Response'}->{'a:DellAsset'}->{'a:Warranties'}->{'a:Warranty'}->{'a:EndDate'});
This is not working. I get an empty page when executing this code. Can anybody explain why? Thanks in advance!
you can used curl, For ex:
$url = "https://api.dell.com/support/v2/assetinfo/warranty/tags?svctags=1RP22W1&apikey=1adecee8a60444738f280aad1cd87d0e"
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5);
$response = curl_exec($ch);
In case you wanted more.
get url: https://api.dell.com/support/v2/assetinfo/warranty/tags.json?svctags="
+tag+"&apikey=1adecee8a60444738f280aad1cd87d0e
Then parse the response in (javascript).
In python you could also read each line page.readlines() and use encode=json.loads(data) to start reading the result like a dictionary.
Related
I am working on a project where I must pull data from an XML API using PHP cURL.
I am unable to retrieve very large data sets from the API, for example I have no issue pulling 500 user email addresses. But if I am pulling a list of 15,000 email addresses I only get about 4,000 back and the request takes about 3 minutes.
I have adjusted the timeout in php.ini which did not help.
My thought is to retrieve the data in batches and then append to the data pulled in. I am pretty new to PHP/back end scripting so forgive me if this seems like a simple question.
My research has shown array_chunk may be useful, but wouldn't that need the full amount of data to chunk anyway? Or could I pull the data in as a chunk, go back to where I left off in the data set and rinse/repeat?
Here is the code I have so far.
function load_users($username, $token, $path, $list_id)
{
$xml = '<xmlrequest>
<username>'.$username.'</username>
<usertoken>'.$token.'</usertoken>
<requesttype>subscribers</requesttype>
<requestmethod>GetSubscribers</requestmethod>
<details>
<searchinfo>
<List>
'.list_id.'
</List>
<Email></Email>
</searchinfo>
</details>
</xmlrequest>';
$ch = curl_init($path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = #curl_exec($ch);
curl_close($ch);
$xml_doc = simplexml_load_string($result);
return $xml_doc;
}
There is a supermarket website and I need to get list of product name and price data.
The website is: http://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler
However, I cannot get this content with success. Every attempt finalized with a null result. I am not familiar with cURL, but it is recommended me to overcome this issue. As I see, the product list is called with Ajax - JSON and for this reason, I should follow requests to see JSON files and their contents using PHP. ...But how?
Thank you in advance.
The code I tried:
<?php
$url="https://www.sanalmarket.com.tr/kweb/sclist/30011-tum-meyveler";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);
var_dump(json_decode($result, true));
?>
Your curl request did work and you are getting html response in the $result variable. The problem is that you are treating the html response string like a valid JSON string.
instead of
var_dump(json_decode($result, true));
try
var_dump($result);
Here $result is not a valid JSON string. It is a string containing the html that the server responded. So you cannot parse it directly into an array or object without using a html parser.
I'm try to read an external XML file with a PHP script, no wmatter what I try I get an "empty document" error. If I open the url in my browser I can access and read the xml fine.
There are numerous other posts on stack overflow with similar problem as mine but none of the solutions work in my case.
This is my code:
$url="http://xml.example.com";
$xml = new SimpleXMLElement("compress.zlib://$url", NULL, TRUE);
parser error : Document is empty in /home/admin/public_html/xml/index2.php on line 4
Apparently the 3rd party service requires that I explicitly request gzip compression.
Any ideas?
Thanks,
Alan.
Ok I got it working using curl then createing an XML object (I think!)
$ch = curl_init();
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
// set url
curl_setopt($ch, CURLOPT_URL, $url);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
curl_close($ch);
//print_r($output);
$oXML = new SimpleXMLElement($output);
The results are compatible with my script and can be parsed to extract the data :0)
I'm trying to figure it out how to take information from another site(with different domain name) and place it in my php program.
Explanation:
User inputs URL from another site.
jQuery or PHP takes information from entered URL. I know where the information is (i know its' divs ID)
And that var is put into my php program as a variable $kaina, for example.
EX:
User enters URL:http://www.sportsdirect.com/lee-cooper-bud-mens-boots-118358
And I want to get the Price. (27,99)
What lang should I use? PHP? or jquery? or anything else?
What function should I use?
How should the program look like?
Thank you for your answers :)
I'd say you have to use php (curl or file_get_contents) to download the page on to your server, parse it or use regular expression to get the price. But in this case it will be even trickier because it looks like this link leads to a page that uses javascript.
But you have to know the format of how you are going to extract the data. So PHP will do the job.
PHP's cURL library should do the trick for you: http://php.net/manual/en/book.curl.php
<?php
$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
You need to research on each of the step mentioned below,
One Thing That you can do is, post the message entered by the user to the server means PHP file, there you can extract the url entered by the user,
In order to extract the URL from the user post, you can use regex search:-
Check this link out:-
Extract URLs from text in PHP
Know you can curl to the url extracted from the user input.
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $extracted_url );
$html = curl_exec ( $ch );
curl_close($ch);
The curl output will contain the complete html of the page, you can then use a HTML parser
$DOM = new DOMDocument;
$DOM->loadHTML($str);
to parse till the required div is found, to have its value.
I would proabaly do something like this:
get the contents of the file: $contents = file_get_contents("http://www.sportsdirect.com/lee-cooper-bud-mens-boots-118358")
convert the contents you just got to xml: $xml = new SimpleXMLElement($contents);
search the xml for the node with attribute itemprop="price" using xpath query
read the contents of that node, et voila, you have your price
I am trying to get the details of a venue in Foursquare using the venue ID, but there is something minor that isn't correct:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.foursquare.com/v2/venues/4c599c84f346c9287ff84cca?client_id=[MY_ID]&client_secret=[MY_SECRET]&v=20120609');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
$result = json_decode($contents);
var_dump($result);
curl_close ($ch);
If I change the URL to something like Google News RSS, or one of my XML feed pages, I get results, to the cURL portion is right
If I paste the above URL into my browser (with my actual ID&Secret) then I get a json formatted result with the data that I expect (correct name of venue, etc). So I know the URL is correct.
If I copy the json from the above process and put it into a variable in my code, then set $result to the decode version of that variable then I see the results properly. So I know that the decode/output bit is working.
Somewhere between retrieving the result and storing it in a variable for decoding something is going wrong. I have to assume it is something silly and simple, since all of the parts are there, but I can't figure it out.
Any help is appreciated.
The answer turned out to be that I had to add the following 2 lines:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);