I want to know if it's possible to read/parse the HTTP response header after a POST request in PHP without the use of cURL..
I have PHP 5 under IIS7
The code I use to POST is :-
$url="http://www.google.com/accounts/ClientLogin";
$postdata = http_build_query(
array(
'accountType' => 'GOOGLE',
'Email' => 'xxxxx#gmail.com',
'Passwd' => 'xxxxxx',
'service' => 'fusiontables',
'source' => 'fusiontables query'
)
);
$opts = array('http' =>
array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Above, im doing a simple ClientLogin Authentication to google and I want to get the Auth token which returns in the header. Echo-ing $result only gives the body content and not headers which contains the auth token data.
The function get_headers() may be the one you are looking for.
http://php.net/manual/en/function.get-headers.php
Use the ignore_errors context option (documentation):
$opts = array('http' =>
array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $postdata,
'ignore_errors' => true,
)
);
Also, maybe use fopen rather than file_get_contents. You can then call stream_get_meta_data($fp) to get the headers, see Example #2 on the above link.
Related
I tried to send a POST request from WordPress to an external API (assign a tag to an user in a CRM system). When I used cURL, everything was OK. Here is the cURL code:
function my_function () {
$body = array ( 'tags' => array ( array (
'email' => 'xxx#gmail.com',
'tag' => 'Customer5'
))
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "$api_url",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($body, true),
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"User-Agent: Your App Name (www.yourapp.com)",
"Authorization: Basic xxxxxx"
),
));
$response = curl_exec($curl);
curl_close($curl);
var_dump($response);
}
add_action( 'init', 'my_function');
But then I switched to use wp_remote_post, I got a "415 - Unsupported Media Type" response.
$body = array ( 'tags' => array (
array(
'email' => 'xxx#gmail.com',
'tag' => 'Customer5'
))
);
$headers = array (
'Content-Type' => 'application/json',
'User-Agent' => 'Your App Name (www.yourapp.com)',
'Authorization' => 'Basic xxxxxx',
);
$request = wp_remote_post('$api_url', $arg );
$arg = array (
'header' => $headers,
'body' => json_encode($body, true),
'method' => 'POST',
'sslverify' => false,
);
echo '<pre>';
print_r($request);
echo '</pre>';
I tried a lot of modifications (changed associative array format to key:value pair, add AddType to htaccess file...), but nothing worked. Please help, I'm stuck
Excerpt from KeyCDN:
A 415 Unsupported Media Type error occurs when the origin server
refuses a particular request since the resource is in a format that is not supported by the server for the HTTP
method
used. This unsupported format type issue can be caused by what is
defined in the resource's Content-Type or Content-Encoding
headers.
And in your case, the error happened most likely because your remote request sent the wrong Content-Type header — which defaults to application/x-www-form-urlencoded when the HTTP method is POST.
And yes, you did include the right Content-Type value in your $headers array. But unfortunately in your $arg array which you passed to wp_remote_post(), you used the wrong array key — header, which should actually be headers (note the "s").
So use headers and not header, just as you can see below:
$api_url = 'your API URL';
$body = array(
'tags' => array(
array(
'email' => 'xxx#gmail.com',
'tag' => 'Customer5',
),
),
);
$headers = array(
'Content-Type' => 'application/json',
'User-Agent' => 'Your App Name (www.yourapp.com)',
'Authorization' => 'Basic xxxxxx',
);
$arg = array(
'headers' => $headers, // good
// 'header' => $headers, // bad; i.e. wrong array key ('header')
'body' => json_encode( $body ),
// 'method' can be omitted since you're using wp_remote_post()
'method' => 'POST',
'sslverify' => false,
);
$request = wp_remote_post( $api_url, $arg );
// ..if the response still isn't good, what's the output of this:
var_dump( $request );
if I use file_get_contents for sending post data to another .php with like the example given by the manual further down, could a visitor of this webpage have any possiblity to view/trace the post data that i send? Thanks
<?php
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
?>
https://www.php.net/manual/en/context.http.php
While trying to fetch the active subscriber count from Sendy API, I am getting following error
The POST request looks like this:
http://my-sandy-Installation/api/subscribers/active-subscriber-count.php
In header: Content-Type:application/x-www-form-urlencoded
In Body:
api_key= mykey
list_id= mylistid
Can anyone please help?
$boolean = 'true';
$postdata = http_build_query(
array(
'email' => $email,
'api_key'=>'your api_key',
'list' => 'Your List id',
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);
I've been trying to send a form my website to another website using this code
$jumlah = $query1->jumlah;
$postdata = http_build_query(
array(
'url' => $url,
'jumlah' => $jumlah
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
Before I send it to that website, I'm trying to send the form to my own website, and it works. I change this code to try send a form there.
file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
After I change that, it doesn't send anything.
update :
here is it the network tab
my goal is to post this data
to this form which is different website, without opening this page
In the request header you can add 'header' => 'Access-Control-Allow-Origin: *' and try. It should work. Even after that request fails means, other server may be blocking it
$jumlah = $query1->jumlah;
$postdata = http_build_query(
array(
'url' => $url,
'jumlah' => $jumlah
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'header' => 'Access-Control-Allow-Origin: *',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://insta.kingdompanel.xyz/addlikes.php', false, $context);
Try the above modified code, if it works fine otherwise, Other server may not allowing CORS requests
Is it possible to make file_get_contents show me the actual response even if an error occurs?
It is difficult to debug otherwise. For instance, say you have the following code:
$url = 'https://api.twitter.com/oauth/request_token';
$data = array();
$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);
var_dump($http_response_header);
This shows me NULL for the result and the HTTP headers, but I want to get the actual message Twitter sends back (should be something like Failed to validate oauth signature and token), which is what you get if you try to load https://api.twitter.com/oauth/request_token in your browser.
There is a very simple switch for the context. just add this line to options:
'ignore_errors' => true
so you will get
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
'ignore_errors' => true
)
);