Receive PHP code with an POST request - php

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.

Related

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/

How to execute URL in codeigniter?

I want to send the SMS to the user using the third party API key in codeigniter. For that I used the file_get_content() But when file runs it takes the more execution time and after display blank page.
I also use CURL but same error in codeignator.
This code runs another PHP script only error in codeigniter
Following my code:
$to = "thisisprashantkumbhar";
$username ='puretechnology';
$numbers = '9326447272';
$messagenew=rawurlencode($to);
$apikey = 'gdfgrte5-er54-h57f-4rgt-0a7215d15abc';
$url = "http://sms1.businesslead.co.in/sendSMS?username=$username&message=$messagenew&sendername=CMISAM&smstype=TRANS&numbers=$numbers&apikey=$apikey";
$response = file_get_contents($url);
return $response;
$stream_options = array(
'http' => array(
'method' => 'GET',
),
);
$context = stream_context_create($stream_options);
$response = file_get_contents("http://api.smsbrain.in/1.2/appsms/send.php?user=username&passwd=141414&senderId=SATISH&recipients=9723613143&message=Hello", null, $context);
echo json_encode($response);
echo $url and it will show you the complete url then execute url you will get the error. e.g invalid key check your details as well.

file_get_contents return all php code

I want to send a POST request to some file when I load index.php. I use this code:
$query = http_build_query(array('ajax' => 'gwonline', 'session' => $current_session));
$contextData = array (
'method' => 'POST',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
"Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
$context = stream_context_create (array ( 'http' => $contextData ));
$result = file_get_contents(PATH::Includes . 'ajax.php', false, $context);
$result_decoded = json_decode($result, true);
echo '<div id="gwonline">' . ($result_decoded['isonlinestr'] ?: '<span class="fa fa-circle" style="color:orange"></span> Unavailable') . '</div>';
The problem is, $result ends with getting the PHP code instead of whatever the file actually printed. How can I fix it without changing the site to http://... as it's not an option for me at the moment.
Use include with output buffering. file_get_contents is reading the file from the filesystem, it isn't going to use PHP to analyze it. The only reason it works when you use http:// is because then the web server is serving the file, not the filesystem.
ob_start();
include "ajax.php";
$result = ob_get_clean();

Retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself

I'm retrieving a PHP file using file_get_content(), cURL() or fopen() all return PHP code itself. It's not retrieving just the HTML code itself that PHP is outputting. How can I overcome this please?
It's basically generating an HTML newsletter preview for my customer that is generated from their CMS. However I want them to be able to copy and paste the HTML it generates into Mailchimp or such like. It just fetches the entire PHP source code though as well as the HTML :(
Hope you can help.
Thanks in anticipation.
Pete
Trying to read a local PHP file will return its source code, since its a "local" file, and is not parsed by the server.
If you wish to read the result of the PHP file you must request it from the server.
Meaning instead of doing:
$data = file_get_contents('x.php');
You should be doing
$data = file_get_contents('http://mysite.com/path/x.php');
Which will be parsed in the server, and then returned to you.
Cheers! :)
Shai.
This will work in localhost. Try this one: $url = "http://localhost/box.php";
function isAllBoxContents($boxid,$pageNum)
{
$postdata = http_build_query( array('myBoxid' => "$boxid", 'isAllBoxContents' => "all",'pageNum' => "$pageNum",'innerReq' => true ));
$opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ));
$context = stream_context_create($opts);
$url = "http://localhost/box.php";
$result = file_get_contents( $url, false, $context);
$cacheFileName = "isAllBoxContents".$boxid.$pageNum;
cacheMe($cacheFileName,$result);
}

Sending httprequest from localhost

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.

Categories