SimpleXMLElement:difference in Curl and file_get_contents - php

So guys, there is another question.
I've fetched a webpage via curl and constructed XML document:
.......
$data = curl_exec($ch1);
$xml = new SimpleXMLElement($data);
And it worked correctly.
After I used file_get_contents like this
......
$file1 = file_get_contents('https://meet77842937.adobeconnect.com/api/xml?action=report-my-meetings', false, $context);
$xml = new SimpleXMLElement($file1);
The last code cannot construct XML, though I able to see through var_dump that it receives a page. And I stumbled: I think curl is advanced version of file_get_contents but they must not differ in fetching page. Where are the problems?
The error:Extra content at the end of the document
I don't know how but webpage returns some symbol at the end
With regards

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,

How do I receive XML data via a webhook in PHP?

I've been using StackExchange for many years but I have a problem I haven't found any answers for elsewhere and I'm hoping for some help.
The PHP program I'm working on is trying to receive an XML from a webhook, take that data, convert it via XSLT, and send via cURL POST the new XML to a third party.
The conversion part works great, but I'm having trouble receiving the data. If I just read from a local XML file, that's all dandy, but if I try to use php://input, the end result is just an empty XML sent to the third party.
Here's the relevant-code snippet: I can give the other stuff if it's any help!
//initialize the xml for the XSLT work
$xml = new DOMDocument;
//the part of the code looking for the data
$input = file_get_contents('php://input');
$xml = simplexml_load_string($input);
//the XSLT stuff is hereout
$xsl = new DOMDocument;
$xsl->load('ConversionSheet.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$XSLTout = $proc->transformToXML($xml);
I'm using XAMPP to host the PHP and go to https://localhost/projectname/filename.php/ in Firefox.

Working with CDATA / Soap / PHP

So, I have this xml file:
xml
I use this code to get this xml on php with soap
$response = $clientrepasse->__soapCall("GetSchema", array($paramsrepasse));
$return = $clientrepasse->__getLastResponse()
When I show this $return with my print_r function I got this
return
When I use function var_dump it shows that the $return is a string(40000) positions.
I tried:
$xml = simplexml_load_string($return);
When I print $xml it's empty, I need to transform that string to xml and then to json, or something like that, because I need to work on that xml.
Any help will be appreciated. Thank you and sorry about my english.
Your XML has namespaces. simplexml does not load XMLs into an XML object when it has namespaces. You have to register your namespace to be able to load the XML using simplexml_load_string.
Check the link below:
XML to JSON
XML to JSON
Post XML to get the answer specific to your XML .
You shouldn't need to call __getLastResponse(). It gives you the raw HTTP body as a string, which isn't what you want.
This should be all you need:
$response = $clientrepasse->GetSchema($paramsrepasse);
print_r($response);
You don't need to work with XML. $response will be a PHP object that you can work with straight away.
Note that you also don't need to call __soapCall() directly either.

Not able to read aliexpress.com via 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.

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.

Categories