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!
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.
Related
So i need to gain access to a web service containing some json, but to do so I was told to make use of PHP POST method to first log into the web service. I was giving an array with 3 types/values.
{
"Username":"user",
"password":"1234",
"LoginClient":"user"
}
I have been searching all day for a solution, but have come up short :(.
Any advice or push into a right direction would be much appreciated.
Hope I have explained this clearly enough.
you could do as follows:
$url = 'http://yourDomain.net/api/auth/';
$data = array('Username' => 'user', 'password' => '1234', 'LoginClient' => 'user');
$opts = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
)
);
$context = stream_context_create($opts); //Creates and returns a stream context with any options supplied in options preset.
$response = file_get_contents($url, false, $context);
var_dump($response);
Or you could read about CURL as another option to make POST requests.
I need to validate some form data both in client-side and server-side (php). I can use some API web service to check the data form. The problem is: I can validate the data, in client side, using an Ajax call and my API web service. How can I use the API web service from server-side?
You can use file_get_contents to invoke external call
$homepage = file_get_contents('http://www.example.com/');
And there you can send the parameter from the url itself,
Here the example to send parameter to your api
Construct an array and place your values to be validated
$getdata = http_build_query(
array(
'name' => 'Francesco',
'phone' => '2434343',
'age'=>'23',
'city'=>'MA',
'state'=>'US'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $getdata
)
);
$context = stream_context_create($opts);
Then send it to your api's directly
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
Well, I know that the headline look simple, but i was looking from 3 days for an example on how to make the POST request to webapi.
Currently I am using JQuery to do my POST, but I need some php script to run and talk to my C# webAPI, and it seems impossible to find some examples or explain on how to do that.
Someone gave me then Code :
$response = file_get_contents('http://localhost:59040/api/Email/SendEmails');
$response = json_decode($response);
echo ($response);
But this one does nothing - Not even an error on how to go more into the problem.
I simpley need a php script to make the POST request to webapi who gets 1 param(String) and return An ok answer or Error,
After Maalls answer from this post How do I send a POST request with PHP?
The answer was really simple and the code was the following :
$url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Thanks Maalls and dbau for the answer :).
I try to make google url shortener with wp_remote_post()
but I got error result,
I know how to use CURL, but CURL not allowed in WordPress!
This resource for API with WordPress:
http://codex.wordpress.org/Function_Reference/wp_remote_post
http://codex.wordpress.org/Function_Reference/wp_remote_retrieve_body
http://codex.wordpress.org/HTTP_API#Other_Arguments
http://codex.wordpress.org/Function_Reference/wp_remote_post#Related
This google url shortener API docs:
https://developers.google.com/url-shortener/v1/getting_started#shorten
This is my code:
function google_url_shrt{
$url = 'http://example-long-url.com/example-long-url'; // long url to short it
$args = array(
"headers" => array( "Content-type:application/json" ),
"body" => array( "longUrl" => $url )
);
$short = wp_remote_post("https://www.googleapis.com/urlshortener/v1/url", $args);
$retrieve = wp_remote_retrieve_body( $short );
$response = json_decode($retrieve, true);
echo '<pre>';
print_r($response);
echo '</pre>';
}
The WordPress API requires that the headers array contain an element content-type if you want to change the content type of a POST request.
Also, it looks like the body of your HTTP request is being passed as a PHP array, not as a JSON string as the Google Shortener API requires.
Wrap the array definition for body in a json_encode statement, and make the headers field a sub-array, and give it a shot:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => json_encode(array('longUrl' => $url)),
);
Alternative, you could just write the JSON format yourself as it is fairly simple:
$args = array(
'headers' => array('content-type' => 'application/json'),
'body' => '{"longUrl":"' . $url . '"}',
);
I want to send a GET request to an external site, but also want to send some parameters
for example i've to send a get request to example.com
i want to execute www.example.com/send.php?uid=1&pwd=2&msg=3&phone=3&provider=xyz
My code is :
$getdata = http_build_query(
array(
'uid' => '1',
'pwd' => '2',
'msg'=>'3',
'phone'=>'9999',
'provider'=>'xyz'
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'content' => $getdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/send.php', false, $context);
I get a server error .
The content option is used with POST and PUT requests. For GET you can just append it as a query string:
file_get_contents('http://example.com/send.php?'.$getdata, false, $context);
Furthermore, the method defaults to GET so you don't even need to set options, nor create a stream context. So, for this particular situation, you could simply call file_get_contents with the first parameter if you wish.