Not working : Using file_get_contents to post data - php

I am learning and I am trying post data to 4.php and get the result to printed on range.php.. I have already seen the How to post data in PHP using file_get_contents?
<?php
error_reporting(-1);
$postdata = http_build_query(
array('var1' => 'sugumar',
'var2' => 'doh')
);
$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://localhost/php/4.php', false, $context);
print_r($result);//DOESN'T PRINT ANYTHING
?>
4.php
print_r($_REQUST);
$postdata = http_build_query($_POST);
print_r($postdata);
I want to know why it's not printing anything... please help me..

I have updated your code using code from the PHP manual and changes like adding PHP tags to 4.php and fixing typo in $_REQUEST.
Here is the updated code, which seems to be working for me:
<?php
$data = array ('foo' => 'bar', 'bar' => 'baz');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('http://localhost/php/4.php', false, $context);
var_dump($result);//prints something :)
?>
4.php
<?php
print_r($_REQUEST);
$postdata = http_build_query($_POST);
print_r($postdata);

Related

PHP: Using file_get_contents to access an API - Empty Response

Sorry for the messy code in advance. I want to write a code which returns me infos from the official Blizzard API, which I can then print out on my homepage. The code doesn't throw any errors but it doesn't print out something either. For Starters:
I would also prefer using CURL, but my homepage is on a Wordpress Hosting Site and I don't know how to install the CURL Library that way
allow_furl_open is on
$url = "https://eu.battle.net/oauth/token";
$data = array('grant_type' => 'client_credentials');
//HTTP options
$opts = array('http' =>
array(
'method' => 'POST',
'header' => array ('Content-type: multipart/form-data', 'Authorization: Basic ' .
base64_encode("$client_id:$client_pass")),
'content' => json_encode($data)
)
);
//Do request
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
$accessToken = $json['access_token'];
$tokenURL = "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-eu";
$opts2 = array('http' =>
array(
'method' => 'POST',
'header' => array('Content-type: multipart/form-data', 'Authorization: Bearer ' . $accessToken),
)
);
$context2 = stream_context_create($opts2);
$json2 = file_get_contents($tokenURL,false,$context2);
$result2 = json_decode($json2, true);
$tokenprice = $result2['price'];
echo "<p>Tokenpreis:" .$tokenprice. "</p>";
I didn't add the $client_id and $client_pass into the code snippet, but this exists obviously. I used this PHP CURL Snippet as template. And this is a short explanation on blizzard's site on how is this supposed to work:
Anyone got any ideas what went wrong? I am really out of ideas here and would love anyone who could help.
Thanks in advance
Based on the curl example from the linked API docs, the content type should be application/x-www-form-urlencoded, and so for the initial request to https://eu.battle.net/oauth/token you should be able to follow the example here.
In your case this will look something like:
$url = 'https://eu.battle.net/oauth/token';
$data = array('grant_type' => 'client_credentials');
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array (
'Content-type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
),
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
Also, in your code you are storing the raw result from the initial request in $json, then the decoded array in $result, but attempting to get the access_token from the initial request, instead of the decoded array.

Not working Using file_get_contents to POST data

I try to get content from https://www.freeformatter.com. I want to get POST data from that site. but my PHP script not working.
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://www.freeformatter.com/html-formatter.html', false, $context);
var_dump($result);
This will not worked. Reason is freeformatter.com is not allowing to give API or POST call. They always return you view-source of html page. This is allow to use from UI but not by API call. I tried with CURL post also. its gives return back view-source of html.
Here is code you can try this works well with different
<?php
$data = array ('htmlString' => '<div class="container"><div class="row"><div class="col-md-8">&encoding=UTF-8', 'indentation' => 'THREE_SPACES');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data['htmlString']) . "\r\n",
'content' => $data
)
);
$context = stream_context_create($context_options);
$result = file_get_contents('https://en99otyaenia.x.pipedream.net', false, $context);
var_dump($result);
Code doesnt have any issue. Issue is with URL.

PHP sends GET instead of POST

I have to post some data, but the same adress have some GET and POST functions. My PHP is sending a GET instead of a POST.
$apiURL = 'https://myAPI.com.br/api';
$data = http_build_query(array('postdata' => 10));
$uriRequest = $apiURL.'/main';
$options = array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
'https' => array(
'header' => 'Content-type: application/x-www-form-urlencoded',
'method' => 'POST',
'content' => $data
),
);
$context = stream_context_create($options);
$result = file_get_contents($uriRequest, false, $context);
if ($result === FALSE) {
return var_dump($result);
}
return var_dump($result);
I know the ssl part it isnt safe, but it is just for prototyping purpose.
I cant get PHP to POST intestead of GET on the adress 'https://myAPI.com.br/api/main'.
Judging from http://php.net/manual/de/function.stream-context-create.php#74795 the correct way to create a stream context for a https secured page is:
<?php
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
As you can see we are using 'http' => array... instead of https.

PHP Return XML with stream_context_create()

I'm using stream_context_create() instead of cURL to make an HTTP post. It works, but file_get_contents() returns a single string of all the data instead of an XML object. How can I modify my code so it returns an XML object and I can access the values individually?
<?php
$url = 'http://www.domain.com';
$data = array( 'name1' => 'data1', 'name2' => 'data2', 'name3' => 'data3' );
$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); // data1data2data3
?>
And the page that returns the XML:
<?php
header('Content-type: text/xml');
$xml = '<result><name1>' . $_POST['data1'] . '</name1><name2>' . $_POST['data2'] . '</data2><name3>' . $_POST['data3'] . '</name3></result>';
echo $xml;
?>
I think that you're looking at this var_dump in your browser, and browser tries to render tags <result> and <name..> as standard HTML.
Please try to run this code, and check the source of your webpage (in Chrome, you can do it by prepending page URL by view-source:)

Pass data to file_get_contents() in PHP

I'm having some problems doing this:
I have to make some reports which display like this:
As you can see, the .php receive by url the id of user (idusuario=14).
Now, i have another .php with this code:
<?php
$idusuario = 14;
$postdata = http_build_query(
array(
'idusuario' => $idusuario
)
);
$opts = array('http' =>
array(
'method' => 'GET',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('ContentEnvio.php', false, $context );
echo $result;
?>
When i echo $result, shoul display like the image above or not ?
The result is this:

Categories