0

I've got following array titled $val in PHP :

Array
(
    [page_id] => 208
    [invite_emails] => 
    [invite] => Array
        (
            [0] => 970
            [1] => 991
            [2] => 992
        )

)

I only want to convert the above array into a valid JSON request and send it to some web service URL.

How should I do it? Please help me.

Thanks in advance.

1

2 Answers 2

1

Your question is rather broad, but to convert a php array into a JSON object is very easy.

$jsonString = json_encode($array);

As for sending it to the URL, this question contains some good info.

Or if you want to use curl, this resource is pretty good.

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

1 Comment

But how could I get as an argument in my WS? Suppose following is the request pattern $application->get('/sendInvitation(/:val)','authorization', function() val must be the array in JSON format.
1

As others have posted, use the following to json encode your array:

json_encode($val);

Now, your problem is sending this to your web service via parameter. The way I generally send complex parameters via GET parameters is by base64 encoding.

$param = base64_encode(json_encode($val));

Now you can send $param just like any other parameter. Take a look at Guzzle for making HTTP requests from PHP.

1 Comment

@PHPLover Second the Guzzle recommend. Dont mess about with cURL or similar your life will be immeasurably improved by using a decent client library and I think Guzzle is the best

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.