dina
dina

Reputation: 111

PHP XMLHttpRequest

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)

Upvotes: 0

Views: 188

Answers (2)

Isaac
Isaac

Reputation: 983

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);

Upvotes: 0

jeyemgfx
jeyemgfx

Reputation: 77

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;
}
?>

Upvotes: 1

Related Questions