Add xmlhttprequest inside a PHP post request - php

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",

Related

Cookies are not being sent with file_get_contents or CURL requests

I am trying to send a file_get_contents request to a URL with POST data and a cookie set.
My code is like that:
$postdata = http_build_query(
array(
'search' => 'test',
'token' => '0'
)
);
// Create a stream
$opts = array(
'http'=> array(
'method'=>"POST",
'content' => $postdata,
'header' => "Content-Type: application/x-www-form-urlencoded\n"."Cookie: session_hash=123456789"
)
);
$context = stream_context_create($opts);
// Open the file using the HTTP headers set above
$file = file_get_contents('https://example.com', false, $context);
echo $file;
This is posting the data as I can see, but the Cookie is not being sent during the request...
I also tried to use the same request but with CURL and I have the same problem with that.
Any help would be appreciated.
Thanks!
The headers may need to be separated by \r\n rather than just \n. You can also use an array, and they'll be sent properly.
'header' => array("Content-Type: application/x-www-form-urlencoded",
"Cookie: session_hash=123456789")

PHP GET request from an API with a client-id without the usage of cURL

I have to send a GET Request within a PHP Script to get data from an API with a certain given client-id without the usage of cURL.
In the terminal I use following command:
curl -i -u “123456-123ea-123-123-123456789:“-H “Content-Type:
application/json” -X GET
“https://api.aHoster.io/api/rest/jobboards/jobs”
(123456-123ea-123-123-123456789 in this command is the given client-id I got)
Everything works fine in the terminal, but now I like to do this GET request in a php script.
How do I add the client-id in this attempt?
<?php
$url = 'https://api.aHoster.io/api/rest/jobboards/jobs';
$client_id = '123456-123ea-123-123-123456789';
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'GET',
)
);
$context = stream_context_create($options);
$result = #file_get_contents($url, false, $context);
if ($http_response_header[0] == "HTTP/1.1 200 OK") {
//...
} else {
// ...
}
?>
Based on your cURL command you want to add an Authorization header:
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n" .
"Authorization: Basic " . base64_encode($client_id) . "\r\n",
'method' => 'GET',
)
);
I'm not sure but you may need a colon showing empty password "$client_id:".
Also, do a print_r($http_response_header); afterwards as it may contain information such as "Authorization Required" or something else useful.

PHP HTTP Request (stream_context_create)

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",

Post data & show source code

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

server result comes with an error

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);

Categories