iam doing a post request in php and the server sends back some text when the post is complete. this is the code:
<?php // Create map with request parameters
$params = array ('username' => 'loginapi', 'password' => 'myapilogin', 'term'=> 'tema' );
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
var_dump($result);
?>
the problem is that the following error occurs eventhough the result i want follows:
Notice: file_get_contents() [function.file-get-contents]: Content-type not specified
assuming application/x-www-form-urlencoded in C:\xampp\htdocs\directory \Search_Result.php on line 49
string(269) " Nandimobile
19 Banana Street, American House East legon
IT Software products and services0302503313 0244709575 "
Thanx in advance
You'll need to specify a content type for your POST.
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
1, You did not receive error as you said, it's just notice (you can disable it in php.ini configuration)
2, You can simply avoid this problem by setting content-type header like this:
$contextData = array (
'http'=>array(
'method' => 'POST',
'header' => "".
"Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n".
"Content-type: "."application/x-www-form-urlencoded"."\r\n",
"content"=> $query )
);
More info here : http://php.net/manual/en/function.stream-context-create.php
add # before file_get_contents and try
$result = #file_get_contents (
'http://infolinetest.nandiclient.com/search/searches/requestData.xml', // page url
false,
$context);
Related
I have this file that send a POST request with PHP to a file inside other server:
// 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);
The file that I'm requesting checks if HTTP_X_REQUESTED_WITH is equal to xmlhttprequest. (this is done because most of the requests sent by that file are through ajax)
So how can I add this kind of thing in my PHP code? to be sent this parameter?
You can add the X_Requested_With header to the headers of the http array like so:
'header' => "Content-type: application/x-www-form-urlencoded\r\n" .
"X-Requested-With: xmlhttprequest\r\n",
I used OAuth for Dropbox to get access token: https://blogs.dropbox.com/developers/2012/07/using-oauth-1-0-with-the-plaintext-signature-method/
But I got error message:
Warning: file_get_contents(https://api.dropbox.com/1/oauth/request_token): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
My PHP Code:
$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);
print_r($Result);
Based on the documentation at http://php.net/manual/en/context.http.php, it looks like the header option is just a normal string, not json_encoded. Alternatively, you should be able to use a numerically indexed array of headers, like so:
$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXXXX\", oauth_signature=\"XXXXX&\"\r\n"
);
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
This is wrong:
$Header = json_encode(array('Authorization: OAuth oauth_version="1.0"', "oauth_signature_method" => "PLAINTEXT", "oauth_consumer_key" => "XX", "oauth_signature" => "XX"));
The header paramter in an http stream is either a simple single string containing one single header key: value, or an array of key: value strings. You're stuffing in a single json string as your header, meaning it's NOT a valid http header.
Remove the json_encode() part completely. $Header = array(...) is all you need.
I don't know if this is possible, i'm having a problem getting the PHP SDK to work and i don't want to send App Access Token to browser.
So i tried the following.
$contextData = array (
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
/* 'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query*/ );
$context = stream_context_create (array('https' =>$contextData));
$url = 'https://graph.facebook.com/userAppId/notifications?access_token=********&href=index.php&template=test';
$result = file_get_contents ($url,false,$context);
echo $result;
Problem: I get this error.
Warning: file_get_contents(https://graph.facebook.com/userAppId/notifications?
access_token=********&href=index.php&template=test):
failed to open stream:HTTP request failed! HTTP/1.1 400 Bad Request in
C:\wamp\app\fb\fb_notificationsphp.php on line 19
Hi I am trying to get the following to work. It is making the HTTP request because I can see it at the other end but it is not passing over the two values and parameters.
Can anyone help?
<?php
$url = "http://example.com/ws.php?uid=0000&pin=0000";
$fields = array(
'target1' => $_GET['target1'],
'target2' => $_GET['target2']);
$data = http_build_query($fields);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $data,
)
));
$result = file_get_contents($url, false, $context);
echo $data."<br />";
echo($result);
?>
If PHP 5.2, see PHP bug https://bugs.php.net/bug.php?id=41051
Description:
the http wrapper ignores 'header' option in stream_create_context( )
if the value is not given as a simple array; neither a string nor a
map (e.g. array('X-My-Header' => 'test')) will cause the additional
headers to be sent.
Based on the examples i've seen you need a \r\n at the end of the header value.
'header' => "Content-type: application/x-www-form-urlencoded\r\n"
apart from that you will also need to provide
"Content-Length: " . strlen($data) . "\r\n",
I have question, is it possible to first post data to a site and then get the source code of the site I was posting the data to?
I tried with this:
$html = file_get_contents('http://test.com/test.php?test=test');
But the ?test=test is $_GET....
So yeah, I hope someone can help me! :) Thanks in advance (Sorry for bad english!).
You can use 3rd parameter of this function: context
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
// edit -little bug should be:
$opts = array(
'http' => array(
'method' => "POST",
'header' => "Connection: close\r\n".
"Content-type: application/x-www-form-urlencoded\r\n".
"Content-Length: ".strlen($postdata)."\r\n",
'content' => $postdata
)
);
You can not get the source, but you can get the page/output as it is provided by the server. As you mention file_get_contents(), you can use it to send a POST-request, but it would look something like this.
// Create map with request parameters
$params = array ('surname' => 'Filip', 'lastname' => 'Czaja');
// Build Http query using params
$query = http_build_query ($params);
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
'http://www.sample-post-page.com', // page url
false,
$context);
// Server response is now stored in $result variable so you can process it
Example from: http://fczaja.blogspot.se/2011/07/php-how-to-send-post-request-with.html