Sending httprequest from localhost - php

function PostRequest($url) {
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Cookie: testcookie=blah; testcookie2=haha;'
)
);
//$context = stream_context_create($opts);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
After I sent out the cookies, I still return by a message non login. but when I surf the pages with browser, I am login.
I sent request with localhost then I tried to used ajax to sent the request, but return status 0......
Is there any way to sent out the request?

If you want to play with HTTP scripting, i have library which you can use. https://github.com/toopay/CI-Proxy-Library, its orriginally written for CodeIgniter, but with little tweak, you should can use it on any PHP script.

Related

Redirect in php server side after http request

I am very new to php so this question might be trivial.
I am trying to understand if it is possible to redirect the browser from php server side to the page returned by HTTP request.
I have a HTTP Post request looking like so:
$postdata = http_build_query(
array(
"someData" => "data" ,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'content' => $postdata,
'header' =>
"Cookie: someCookie" .
'content-type: application/x-www-form-urlencoded; charset=utf-8'
)
);
$context = stream_context_create($opts);
$result = fopen('myWebsiteUrl', 'r', false, $context);
var_dump(stream_get_contents($result));
In the post I am being redirected to a different page with Get.
I am trying to force the browser to move to the 'redirected' page. With the above code I am getting back the html of the redirected page but what I'm after is an actual redirect.
The option of retrieving the redirect URL and doing the redirect myself in PHP doesn't work because the Get request has to happen within the same session as the Post.
header('Location: whereveryouwantogo.php?get=123456');
does a redirect for you with a GET-parameter attached. I'm not sure if I really got your question right, but I'm pretty sure you could do what you want/need with the header-command.

Posting a Curl request through php

I wanted to send a post request to pilosa database. The request is like this -
curl localhost:10101/index/user/query
-X POST
-d 'Bitmap(frame="language", id=5)'.
How can i send the following request through php ?
Link for referrence : https://www.pilosa.com/docs/api-reference/
If you don't have the php curl library available to you, you can query Pilosa with php's file_get_contents which is part of core php. The following php script should perform your example query:
<?php
$url = 'http://localhost:10101/index/user/query';
$data = 'Bitmap(frame="language", id=5)';
$options = array(
'http' => array(
'method' => 'POST',
'content' => $data
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
?>
The Pilosa HTTP API documentation can be found at: https://www.pilosa.com/docs/api-reference/

Receive PHP code with an POST request

I need to send a POST request to another file called global.php, for this I try this code below:
$url = 'global.php';
$data = array('stack' => 'overflow');
$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);
This is the global.php file that should process the request:
if(isset($_POST['stack'])){
echo 'exists';
}else{
echo 'error';
}
The problem is that instead of the command var_dump ($ result); show exists, it shows the PHP code? how can I solve this problem?
And why when I try to do the same thing using ajax it returns me the text exists and not PHP code?
You should use full url, to process php file through server.
$url = 'http://YOURURL.com/global.php';
AJAX call is made from browser, to absolute URL, thats why You are getting desired response.

Call a PHP from another PHP (without cURL)

I have a HTML page that has to call a PHP on another domain. The "Same-Origin-Rule" of most browsers prohibits that call. So I want to call a PHP on my domain to call a PHP on the target domain. I want to avoid cURL so I decided to use fopen in that pass-through PHP using $context:
$params = array('http' => array('method'=>'POST',
'header'=>'Content-type: application/json',
'content'=>json_encode($_POST)));
$ctx = stream_context_create($params);
$fp = fopen('https://other_domain.com/test.php', 'rb', false, $ctx);
$response = stream_get_contents($fp);
echo $response;
But the incoming $_POST in test.php seems to be empty. Any ideas?
Try to build params with http_build_query()
$postdata = http_build_query(
array(
'json' => json_encode($_POST),
)
);
and then
$params = array('http' => array('method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded',
'content'=> $postdata));
On the other site get it via $_POST['json']
Unless you have a server that supports application/json as a POST content type, your code isn't going to work: HTTP servers expect POST data to always be one of application/x-www-form-encoded or multipart/form-data. You need to rewrite your code to send the POST data in one of the supported types.
I managed it this way:
$postData = file_get_contents('php://input');
$params = array('http' => array('method'=>'POST',
'header'=>'Content-type: application/x-www-form-urlencoded',
'content'=>$postData));
$ctx = stream_context_create($params);
$url = 'https://other_domain.com/test.php';
$fp = fopen($url, 'rb', false, $ctx);
$response = stream_get_contents($fp);
echo $response;
This easily hands trough all incoming POST data and also forwards any responses. Thanks for all your posts!

php http request

$post_data = array(
'url' => $all[2],
'op' => 'sv',
'sid' => 1
);
// Send a request to example.com
$result = post_request('http://www.yahoo.com', $post_data);
function PostRequest($url) {
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-type: application/x-www-form-urlencoded\r\n"."Accept-language: en\r\n" .
"Cookie: member_id=8593099\r\n" .
"Cookie: pass_hash=fad917fe75e1059f85fc6d9bb6f7a19f\r\n".
"Cookie: session_id=279fe56fd87e5371dc7e1c9f66c27522"
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
I am able to send the request, but my action needs login to be performed.
Even once I'm logged in, it classifies me as not logged in.
I'm using localhost to send out the request. Is that because of the different domain?
I already copied the login cookies for my localhost, but it is still not working.
Any ideas?
What I tried to do is send http request with php.
My request has sent out, but my destination cannot detect cookies, and claim I am not login.
I'm not too sure what you're trying to accomplish, as the code you've posted isn't quite clear. post_request() isn't a native PHP-function, so you'd have to give us a sample of it for us to be able to help you further.
I would however recommend that you've check that you've put session_start(); way up top in your PHP-files - it ensures that you're able to access the session/cookie.

Categories