1

I'm trying to access a webservice. I'm already using this webservice from an android app but now I need to access some functions from a php document. If I use chromes advanced rest client application for testing it works fine if I select the POST option and application/x-www-form-urlencode as content-type. But when I try to access the webservice from my PHP file I get the response from the server that it can't find the value "tag". This is the code:

$data = array( 'tag' => 'something');

$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Content-Type: application/x-www-form-urlencode")
);

$context = stream_context_create($options);
$url = 'myurl';
$result = file_get_contents($url,false,$context);
$response = json_decode($result);

What is wrong with this code?

Thanks for any help!

1 Answer 1

1

Try this:

$data = http_build_query( array( 'tag' => 'something') );

As defined here, "Content" value must be a string: http_build_query generate the URL-encoded query string you need.

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

2 Comments

Thank you for the information. I tried with http_build_query but still my $_POST['tag'] call in my server side code is empty :(
Ah I found the mistake: I had a typo in my header definition: T of Type should be small and it is urlencoded not urlencode. This in addition with http_build_query works! Thanks!

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.