Not able to read aliexpress.com via php - php

I'm trying to read aliexpress.com deals page via php. I'm not able to get the details of the page in output.
Is there a way I can get the details.
Below is the code.
<?php
include('simple_html_dom.php');
$url = 'http://activities.aliexpress.com/superdeals.php';
$xml = file_get_html($url);
//$file = 'output1.txt';
$element = $xml;
echo $element;
?>

This website used AJAX
There are two solutions :
- Make the same request as Javascript
- Using a tool like Phantomjs
If you look at requests, you will find easily that a GET request is made and return all information in JSON.
So you need to find by yourself the link or use third party library + tools.
EDIT:
You can use your web browser to get the link (I don't give you it because i think stackoverflow is not for that) with firebug or if you're using chrome, in network tab search for the JSON.
$url = "....";
$str = file_get_contents($url);
if($str) {
$json = json_decode($str, true); // json is an array
// ... do what you need
}
I recommend to use curl instead of file_get_contents for many reasons.
Or you can use Phantomjs (it's really more difficult) and get a "HTML snapshot" and then use DOM or XPATH to get what you need, but you must run Phantomjs and use a third party library for communicate with it.

Related

How to read JSON file with PHP from instagram __a=1 page

I’m trying to call a URL and read its content using php.
when I place the url directly in the browser It works, but when I use php it returns empty string.
here’s my code:
$url = "https://www.instagram.com/instagram/?__a=1"
$json = file_get_contents($url);
$res= json_decode($json);
var_dump($res);
Does anyone have an idea why I can’t read the file with PHP?
Thank you,

Any idea how to connect to this API?

I tried to find any widget to show Tinkoof's bank currency rate, because it changes every 60sec., but nothing.
Finally I found this API, but there is no any documentation for it.
I tried to find any articles about parsing, but I guess there's no use in that because of absence of any tag.
I need to show the currency rate on my website via this API. Any idea?
Big thanks!
You just need to fetch the content You can use cURL or file_get_contents()
cURL version:
<?php
$url = "https://www.tinkoff.ru/api/v1/currency_rates";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$r = curl_exec($curl);
curl_close($curl);
$array = json_decode($r, true);
echo "<pre>";
print_r($array);
echo "</pre>";
?>
file_get_contents version:
<?php
$r = file_get_contents('https://www.tinkoff.ru/api/v1/currency_rates');
echo "<pre>";
echo print_r(json_decode($r, true));
echo "</pre>";
?>
Both of them will work unless the remote website requires you to be human (has extra verifications to stop robot requests). cURL would be a better way if that were the case because you can fake a user agent using a header array.
Once you have the array build it's just a matter of accessing the required data. using $r as an array result of the remote json structure.
It looks pretty straightforward to me. For someone with a decent knowledge of PHP will do this, provided the output is:
Now with the above information, I would:
Get the result to PHP using file_get_contents().
Parse the result as an array using json_decode($contents, $true).
Using the above result, I would get display the value using: $output["payload"]["rates"][0]["buy"] or something similar.
At this time of writing, the above will get me 58:

Scraping the page Number of Channel Advisor using PHP

I'm trying to extract the page number of a ChannelAdvisor Inventory List but it always output EMPTY.
here is my code:
<?php
$url = "https://merchant.channeladvisor.com/AM/MyInventory/View_Inventory.aspx?apid=32001263";
$str = file_get_contents($url);
preg_match('/<span id="Surround_ctl00_Surround_Content_Paging_ctl00_l1"> of (.*?)<\/span> /',$str,$as);
var_dump($as);
?>
I'm trying to extract the 340..
You will probably want to investigate Curl, the address you are trying to access is HTTPS and requires authentication. This is probably why you aren't getting back any data.
PHP CURL

Parse an XML webpage using PHP

How do I use PHP to parse XML on the web?
Here is the example XML. How would you get the value of the first "AskRealtime"?
I am aware of SimpleXML. I am looking for the proper code to open the XML webpage, and work with it using SimpleXML.
This suggested code does not succesfully load the page:
$xml = #simplexml_load_file("http://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20AskRealtime%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22A%22%2C%22AA%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
$price = $xml->results->quote[0]->AskRealtime;
echo $price;
Have you tried using SimpleXMLElement? It works just like your code but it's constructed differently.
$url = 'http://query.yahooapis.com/v1/public/yql?q=select%20symbol%2C%20AskRealtime%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22A%22%2C%22AA%22)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
$xml = new SimpleXMLElement($url, null, true);
echo $xml->results->quote[0]->AskRealtime;
There are different methods for getting the file contents, even though I doubt it's the problem.
/* cURL */
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
curl_close($curl);
/* Alternative */
$contents = file_get_contents($url);
Can you tell what is the error you get? Or try var_dump($xml) and see what it returns.
Step 1. - load the file in SimpleXML $xml = #simplexml_load_file($fl) or die($errorMsg);where $fl is the file URL and $errorMsg is the error message to display.
Step 2. - get a content $whatever=$xml->results->quote->AskRealtime[0]; Explained: it goes to the XML, then what is named "results", then what is named "quote", then what is named AskRealtime, but the first (index 0) of it.
And a note: if, for some reason, you'll have to get something which is named whatever-else (so it has a - in the name), then it works only if you make the code $xml->{'whatever-else'}
Use can use some HTTP request library to get the file, e.g.: http://www.php.net/manual/en/function.http-get.php
Then feed the response to SimpleXML.

Reading REST API Response in PHP

I am trying to read Raven SEO Tools API. It is a REST API and currently it is serving the data backup as an XML (or JSON if I choose) when I just request the URL through a web browser. What is the best method to get the response from their server into my own PHP script for me to then play around with.
Any help much appreciated
Cheers
If you only needs to retrieve a URL and parse its info. The easiest way is curl/JSON combination. Note that parsing JSON is faster than parsing XML.
http://www.php.net/manual/en/function.curl-exec.php
http://www.php.net/manual/en/function.json-decode.php
Something simple as:
$url = "http://api.raventools.com/api?key=B1DFC59CA6EC76FF&method=domains&format=json";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 4);
$json = curl_exec($ch);
if(!$json) {
echo curl_error($ch);
}
curl_close($ch);
print_r(json_decode($json));
But if you need to call other methods from this API such as DELETE/PUT, etc. Then to have a REST client in PHP is more elegant solution. A comparison on those clients can be found in PHP REST Clients
I founded this code specifically for Raven API https://github.com/stephenyeargin/raventools-api-php
Sample code:
require 'path/to/raventools-api-php/raventools-api-php.class.php';
$Raven = new RavenTools( 'B1DFC59CA6EC76FF' );
$method = 'domains';
$options = array('format'=> 'json');
$responseString = $Raven->getJSON($method, $options);
print_r(json_decode($responseString));
cUrl
cUrl is a command line tool for getting or sending files using URL syntax.
curl -o example.html www.example.com
file_get_contents
<?php
$homepage = file_get_contents('http://www.example.com/api/parameters');
echo $homepage;
?>
Pecl's HTTPRequest class is a very nice client, I've been using it for a couple of Projects. http://pecl.php.net/package/pecl_http
Another pretty cool client is the Buzz client https://github.com/kriswallsmith/Buzz
It also plays nice with Symfony2 if that's of interest to you :)
You can use either one of them, but I think JSON is the easiest and more hassle-free, unless you use SimpleXML. The decision depends on the complexity of your data.
Given that the JSON returned by the API is valid you can convert it to an array or object by using PHP's json_decode() function.
<?php
# retrieve JSON from API here...
# i.e. it is stored in $data as a string
$object = json_decode($data);
$array = json_decode($data, true);
?>
In SimpleXML, it would be as follows:
<?php
$object = simplexml_load_string($data);
?>

Categories