0

I'm really newby in php and I want to make an XMLHttpRequest in my php page and don't know who can I do it.

This is the Chrome info, as you can see the header is XMLHttpRequest : Chrome Request Image

This is the response data (a table): Chrome XHR response

How can I made this request in my php page??

PD - sorry for my poor english)

2
  • are you sure you want to use PHP for making that request , as I will use ajax for the same. In case you want to check an example, have a look at w3schools.com/php/php_ajax_xml.asp Commented Sep 26, 2014 at 11:27
  • @Satya Thanks, don't know really well what have to use :S I have a php page and I only want to get this table that you can see at the 2nd link from a website who is not mine and don't know how can I do it Commented Sep 26, 2014 at 16:19

2 Answers 2

1

Even if I don't know any details, mostly SimpleXML can help :)

For this feed.xml-Document:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<items>
    <title>Cities</titel>
    <item>
        <cityname>Genf</cityname>
    </item>
    <item>
        <cityname>Köln</cityname>
    </item>
</items>

Try:

<?php
$xml = simplexml_load_file("feed.xml");
foreach ($xml->item as $item) {
    echo $item;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use curl if you wanna do an XMLHttpRequest using PHP. By putting the right header you will make look like you're doing a XMLHttpRequest

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return result as a string
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Host" = > "someloginserver.com",
    "User-Agent" = > "Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
    "Accept" = > "application/json, text/javascript, */*; q=0.01",
    "Accept-Language" = > "en-us,en;q=0.5",
    "Accept-Encoding" = > "gzip, deflate",
    "Accept-Charset" = > "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
    "Keep-Alive" = > "115",
    "Connection" = > "keep-alive",
    "X-Requested-With" = > "XMLHttpRequest",
    "Referer" = > "http://remote/"
));
// Execute curl
$result = curl_exec($ch); // The output will be stored $result
// Check Error
if ($errno = curl_errno($ch)) {
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}";
} else {
    echo "<h2>Request Completed</h2>";
}
curl_close($ch);

2 Comments

works for me!!!! but when I call curl_exec the result is shown in my webpage and I only want the table (XHR data) in plaintext to extract some data, is there any way?
@dina I just edited my answer you need to add the CURLOPT_RETURNTRANSFER options and set it to true. Then you'll have the result in plaintext inside the $result variable. From that you'll be able to do any extraction work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.