I want to use the servicesidekick API using php, as per their documentation at here: http://www.servicesidekick.com/help/api
They are suggesting to use curl for this. I am suppose to get xml file in response.
Below is my code:
exec('command',$data);
when I print the $data then its giving each line of xml in each element of Array. But i want it to be accessed as the XML file.
You can use the SimpleXML functions to make things easy. Here is a very basic example that prints out some comma-deimited fields:
<?php
$token = "123xxx";
$account = "myaccount";
$url = "https://{$token}:{$token}#{$account}.servicesidekick.com/jobs.xml";
$xml = simplexml_load_string(file_get_contents($url));
foreach ($xml->children() as $job) {
echo "{$job->id},{$job->name},{$job->{"job-number"}},{$job->{"balance-remaining"}} \n";
}
?>
It's pretty easy to get access to all of the fields. This example just demonstrates jobs, but all of the entities can be called the same way. Good luck!
Please read the docs of the PHP curl bindings.
There is no function curl() which does lots of magic like you apparently assume in your code.
Related
I'm attempting to use the Zoopla API (http://developer.zoopla.com/docs/read/Property_listings) to output specific data.
I have tested the API using a simple echo after the "file_get_contents() method, which shows the data. Example code shown below (API Key Removed)
$url = "http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF11&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$zoopla = file_get_contents($url);
echo $zoopla;
What Im trying to code is a loop that will allow me to add html tags so that I can style them. I've done similar for a RSS feed but can't figure out a way for this XML.
I have also tried an alternative approach using simplexml_load_file()
$xml = simplexml_load_file($url);
$agent_address = $xml->agent_address->agent_address[1]->agent_address;
echo $agent_address;
Any help would be greatly appreciated!
I found the answer to my own question!
Basically the $URL is a string and not a file "simplexml_load_file()"
So first, we need to get the xml file as a string and then parse the file. Code as followed! Works like a treat!
$zoopla = file_get_contents('http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF64&api_key=xxxxxxxxxxxxxxxxx');
$properties = simplexml_load_string($zoopla);
echo $properties->listing[2]->agent_phone;
UPDATE: The source code is very much different from what Developer Tools shows.
Check out the source: view-source:http://www.machinerytrader.com/list/list.aspx?ETID=1&catid=1002
Is that javascript that needs to be rendered by a browser into html? If so, how can I have php do that process so that I have Html to parse? It's weird that you can use Xpath Checker to return the items I'm looking for (see below), but you cannot access the full html!
(Xpath: //table[contains(#id, 'ctl00_ContentPlaceHolder1') and (contains(#id,"tblContent") or contains(#id,"tblListingHeader"))])
END UPDATE
I need to scrape some information off of this site for work on a regular basis. I am attempting to write some PHP code to scrape this data. I think I have some namespace issues here, having read a number of other posts on SO. I have never encountered namespace problems before and used the approach shown on another SO post (to no avail :().
It appears the xpath query is just not happening for whatever reason. If you have any guesses or solutions as to how to handle this issue, I am open for suggestions.
Also here is the output from my code:
object(DOMXPath)#2 (0) {
}
Debug 1
array(0) {
}
array(0) {
}
I left out the bottom of the code where I var_dump testarray and create and var_dump otherarray. Their output is included above. Obviously the two arrays will be empty if the DOMXPath element has length 0 as well.
$string = 'http://www.machinerytrader.com/list/list.aspx?ETID=1&catid=1002';
$machine_trader = file_get_contents($string);
$xml = new DOMDocument();
$xml->loadHTML($machine_trader);
$xpath = new DOMXPath($xml);
$rootNamespace = $xml->lookupNamespaceUri($xml->namespaceURI);
$xpath->registerNamespace('x', $rootNamespace);
$tableRows = $xpath->query("//x:table[contains(#id, 'ctl00_ContentPlaceHolder1') and (contains(#id,'tblContent') or contains(#id,'tblListingHeader'))]");
var_dump($xpath);
$testarray = array();
$otherarray = array();
foreach ( $tableRows as $row )
{
echo "Debug 1"."\n";
$testarray[] = $row->nodeValue;
}
This is not an XPath issue insofar that the actual content is found from a form post, which you didn't reach yet. JS Source code here does nothing more than authenticate a proper 'user' for the information request, and then send the request via form submission.
At each request, the salt / encryption 'key' is randomized and changes, preventing simple scrapes.
You could rewrite that JavaScript to PHP and then issue two requests, battling the authentication process along the way.
Or, rather than diddle with reverse-engineering this, you could switch your scraping to NodeJS and use something like PhantomJS since it can evaluate javascript but give you programmatic access. Given the complexity of this task, it'd be much simpler to use the right tool.
I'm trying to get the $xml->entry->yt:statistics->attributes()->viewCount attribute, and I've tried some stuff with SimpleXML, and I can't really get it working!
Attempt #1
<?php
$xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos?author=Google");
echo $xml->entry[0]->yt:statistics['viewCount'];
?>
Attempt #2
<?php
$xml = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos?author=Google");
echo $xml->entry[0]->yt:statistics->attributes()->viewCount;
?>
Both of which return blank, though SimpleXML is working, I tried to get the feed's title, which worked!
Any ideas?
I've looked at loads of other examples on SO and other sites, but somehow this isn't working? does PHP recognize the ':' to be a cut-off, or am I just doing something stupid?
Thank you, any responses greatly appreciated!
If you just want to get the viewcount of a youtube video then you have to specify the video ID. The youtube ID is found in each video url. For example "http://www.youtube.com/watch?v=ccI-MugndOU" so the id is ccI-MugndOU. In order to get the viewcount then try the code below
$sample_video_ID = "ccI-MugndOU";
$JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$sample_video_ID}&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
echo $views;
I would use the gdata component from the zend framework. Is also available as a separate module, so you don't need to use the whole zend.
The yt: prefix marks that element as being in a different "XML namespace" from the rest of the document. You have to tell SimpleXML to switch to that namespace using the ->children() method.
The line you were attempting should actually look like this:
echo (string)$xml->entry[0]->children('yt', true)->statistics->attributes(NULL)->viewCount;
To break this down:
(string) - this is just a good habit: you want the string contents of the attribute, not a SimpleXML object representing it
$xml->entry[0] - as expected
->children('yt', true) - switch to the namespace with the local alias 'yt'
->statistics - as expected
->attributes(NULL) - technically, the attribute "viewCount" is back in the default namespace, because it is not prefixed with "yt:", so we have to switch back in order to see it
->viewCount - running ->attributes() gives us nothing but attributes, which are accessed with ->foo not ['foo']
I know AJAX will not be what it is called, but I am looking for something similar that can be done from within PHP itself (not using javascript).
Basically, as the PHP is creating the page, I want to query an API to gather some information that will be used on the current page. Is this possible, and if so what would be the best method?
Thanks
You can use fopen or curl:
Here is an example to open a connection to twitters API, read the public timeline and output parts of it.
<?php
$fp = fopen("http://api.twitter.com/1/statuses/public_timeline.json?count=3&include_entities=false","r");
while($data = fgets($fp))
{
$json .= $data;
}
$arr = json_decode($json);
print_r($arr);
?>
You are probably wanting curl.
http://php.net/manual/en/book.curl.php
If you're talking about a remote HTTP API, you would utilize cURL at runtime. It's basically PHP's way of accessing information across domains.
The above can be a little overwhelming - check out the simple example page to get you started. If available, I suggest working with cURL from the command line, as there's quite a few options on there. It's a good way to get familiar with the command and different options available.
Does anybody have a PHP example of using the VirusTotal.com public API for URL scanning?
The API is available here:
http://www.virustotal.com/advanced.html
There's a Python/JSON example, but I'm not experienced with either :(
All you need is to retreive the report json using file_get_contents this way :
$json = file_get_contents('https://www.virustotal.com/api/get_url_report.json');
Then use json_decode to convert your json into a php array:
$array = json_decode($json);
to see results :
var_dump($array);
to post data use curl, related question.
A PHP example is available on that page (maybe it's new).