0

I have a working cURL function that allows me to bypass the Cross Domain issue, this is the code below

<?php

$url = "http://explorerapi.barratthomes.co.uk/v2.0/development/getbyitemkey?ItemKey=H618701&Auth.Utc=2015-07-31T08:30:26.761Z&Auth.RequestId=a22a17d8-8d62-4954-8a9c-79e5c244c308&Auth.DeviceId=23a5bb10-c646-47c4-8fda-1b6f1d528de3&Auth.Hash=052DAA8E425F143D4B5C55A1EAC87C5D&BrandCode=BAR&ApplicationId=ApplicationId&ApplicationVersion=1.2.3.4&LanguageCode=en-gb&IsPublished=true&MarketingSuiteDevelopmentId=MarketingSuiteDevelopmentId&UserLocation=UserLocation&Os=Android&ScreenResolution=1024x768&Hierarchical=True";

        $cu = curl_init();
        curl_setopt($cu, CURLOPT_URL, $url);
        curl_setopt($cu, CURLOPT_HEADER, false);
        curl_setopt($cu, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($cu, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($cu, CURLOPT_CONNECTTIMEOUT, 10);

        $result = curl_exec($cu);

        if(curl_errno($cu))
        {
            echo "<h2>Unable to connect to site</h2>";
        }

        curl_close($cu);
echo $result;

?>

This works fine when I set the url as seen here. However The url is dynamic and is different each time, therefore I have a JavaScript file which creates the URL and puts it into a variable.

I am having trouble getting the JavaScript variable into PHP so I can use the URL. I have been trying with $_GET and $_POST but having no luck.

Here is my Java for reference the address variable is coming from my JavaScript file that creates the URL.

<!DOCTYPE>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="js/auth.js" type="text/javascript"></script>
        <meta charset="UTF-8">

    </head>
    <body>
        <script type='text/javascript'>

            var url = address;

        $(document).ready(function() {

            window.location.href = "test.php?url_=" + url;

            $.ajax({
                url: "http://127.0.0.1:4001/Barratt/test.php",
                //dataType: 'json',
                //method: 'GET',
                //contentType: 'application/json'
            })
            .done(function(data) {
                console.log(data);
                  //var response = $.parseJSON(data.contents);
                $('#value').html(data);
                  })
            .fail(function() {
                alert('failed to fetch data')
            });
        });   

        </script>
        <div style="width: 100%; height: 100%; border: 2px solid red;" id="value">

        </div>
    </body>
</html>

2 Answers 2

1

I think probably because of this:

in ur JS code, u need to pass a parameter to the server like:

    $.ajax({
        url: "http://127.0.0.1:4001/Barratt/test.php",
        dataType: 'json',
        data:{url:'http://your.url.here'},
        //better to use post in case ur url got special characters
        method: 'POST', 
    });

Then in ur php code, u will be able to get the url using $_POST[]:

$url = $_POST['url'];

Then i think that will do.

Sign up to request clarification or add additional context in comments.

1 Comment

That's done the trick, thanks very much. Made my Friday much better :)
0

I'm not sure if I see it right, but I don't see the super globals $_GET or $_POST being used in your php script.

Are you aware that if you POST a value to a php file it will end up in the super global array $_POST. (and the same for $_GET)

So if you call my.php?value=test it will end up in:

$get_value = strip_tags($_GET['value']); // The strip tags is just some safty thing
if (isset($get_Value))
     // do things with your get_value here

2 Comments

I've tried that and for some reason the API doesn't like the URL, I get a NoCallContext error from the server
@Pedroson and the url is filled? also, is the url url_encoded? else considder using method : "POST", data : {"url" : url} Chen's answer here descibes that post method better.

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.