0

Im working on this DUDAMOBILE API. Custom integration in PHP website. first it takes URL from customer.

$url = $_POST["url"]

and i want to assign this url in below code

$data = '
        {   
        "site_data":
            {               
                "original_site_url":"http://www.test.com/"
            }
        }
    ';

but not sure how to assign it to above code. i tried like this. but it doesn't work

$data = '
        {   
        "site_data":
            {               
                "original_site_url":'.$url.'
            }
        }
    ';

im getting this error Failed to parse JSON: Unexpected character ('h' (code 104))

1
  • 2
    NO! don't create json string manually, use json encode, that's why this exists Commented Mar 6, 2015 at 7:56

2 Answers 2

4

It's because of the slashes in the url. It's better to use json_encode rather than trying to format the json by hand.

$data = ['site_data' => ['original_site_url' => $url]];
$json = json_encode($data); // json_encode($data, JSON_PRETTY_PRINT) to keep formatting.
Sign up to request clarification or add additional context in comments.

1 Comment

ah right. that is the code they gave me to download as API. so i didnt try to change it much
1

You need quotes around the value as well, otherwise it is no valid JSON string:

$data = '
        {   
        "site_data":
            {               
                "original_site_url":"'.$url.'"
            }
        }
    ';

The error is because the parser expects a double quote and finds an "h" of the beggining of the url (http....).

1 Comment

While the answer of kylehyde215 is more clean. If an answer works for you and you think it solves your problem you should mark it accepted.

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.