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
)
);
Related
I want send a get request with file_get_contents. This code returns failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found.
How can I solve it?
<?php
define('API_KEY','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
$data = http_build_query(
array(
'name' => 'test',
'count' => '1000',
)
);
$url = 'https://www.example.com/order';
$options = array('http' => array(
'method' => 'GET',
'content' => $data,
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);
$context = stream_context_create($options);
$response1 = file_get_contents($url, false, $context);
print_r($response1) ;
Also I tried this bottom code, but it returns the error again!
$url = 'https://www.example.com/order?name=test&count=1000';
$options = array('http' => array(
'method' => 'GET',
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);
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
I get quite annoyed by logging in to SO sometimes and i'd like to do it by just calling a PHP file. I tried doing so by sending a post request like this:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head&returnurl=http%3a%2f%2fstackoverflow.com%2f';
$data = array('email' => 'mymail#gmail.com', 'password' => 'mypasswort');
$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);
?>
I also tried including more values like ssrc and fkey but i just shows the SO mainpage after submitting. It even stays at localhost (or wherever the script is running). If i enter a wrong password it marks it as incorrect - so it has to work at lest in some way (the data verification)...
You must send post data:
isSignup:false
isLogin:true
isPassword:false
isAddLogin:false
hasCaptcha:false
fkey:922b6dc5a5a375283c44e298246d7763
ssrc:head
email:asd
password:
submitbutton:Log in
oauthversion:
oauthserver:
openidusername:
openididentifier:
You should send first GET request and parse fkey from result page (this is CSRF protection).
fkey you should send with POST request with post data which I show you.
Simple example:
<?php
$url = 'https://stackoverflow.com/users/login?ssrc=head';
$data = array(
'email' => 'mail#email.z',
'password' => 'pass',
'isSignup' => false,
'isLogin' => true,
'isPassword' => false,
'isAddLogin' => false,
'hasCaptcha' => false,
'fkey' => '',
'ssrc' => 'head',
'submitbutton' => 'Log in',
'oauthversion' => '',
'oauthserver' => '',
'openidusername' => '',
'openididentifier' => ''
);
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
preg_match('~name\=\"fkey\"\svalue=\"([\w]+)\"~', $result, $matches);
$data['fkey'] = $matches[1];
$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);
But I recommend for you use SO API and you willn't have any problems
If you will have problem, write
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.