I'm trying to send a special character, such as ñ, through a POST request in PHP. When I do it, it comes out as ñ, what is wrong and how do I fix it?
I'm sending and receiving the post request in PHP, here is what I use to send it:
$url = '<url>';
$data = array('key' => 'ñ');
$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);
echo $result;
Thanks in advance!
Try using this:
$data = array('key' => urlencode('ñ'));
And in the $url file:
$_POST['key']=urldecode($_POST['key']);
This is how I use to send special characters in GET and POST method with ajax, it must work for php too.
Related
I'm trying to get some json data from another server that needs 3 parameters so that I can access it. I saw this code in internet but its not working. I have searched a lot and I haven't found a solution. When I run this code I get :
use pass and code are missing
(is the response from the server I'm requesting), I hope I made things clear if there's anything I didn't explain please do tell.
$url = 'http://xxxx/.js';
$data =http_build_query(array('user' => 'xx',
'pass' =>'xxx ',
'code' =>'xxx')
);
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $data
)
);
$context= stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { }
echo $result ;
Try to change your payload to json...
$data = json_encode( array(
'user' => 'xxx',
'pass' => 'xxx',
'code' => 'xxx'
) );
and to remove \r\n from your content type
"Content-type: application/json"
I've been looking for a solution for such a long time for my issue.
But i just can't find any solutions and i'm not sure what exactly to search for.
I'll explain my problem here.
Currently i have this PHP script which works perfect and sends a successfully request and receives the respond.
<?php
$url = 'http://example.nl/index.php';
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL');
// 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);
var_dump($result);
?>
But i really need help with sending a request with the following content:
See screenshot: http://i.imgur.com/VgedJes.png
I need to add the 3 headers and i'm not sure how to tell a field/value is empty as you can see in the screenshot.
I just can't figure out how to do this.
I would really appreciate it if someone could help me out with this!
To Add Header :
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\nX-Requested-With: XmlHttpRequest",
'method' => 'POST',
'content' => http_build_query($data),
),
);
Keep in mind that X-Requested-Header can only be interpreted by your PHP application
To send a parameter with an empty value you can do as follow
$data = array('url' => 'http://www.myip.nl/', 'verkort' => 'Verkort URL', 'notMandatoryParam' => '');
Also in your PHP code, you must do a verification : if(empty($_POST['notMandatoryParam'])) {/*SKIP ME*/}
I am trying to send a POST but I am getting an empty post on my other script.
What I am doing wrong?
if(isset($_POST)){
$url = 'http://localhost:3000/post';
$data = $_POST;
$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);
}
This is for sync between 2 apps.
When I replace
$data = $_POST;
by
$data = array('key1' => 'value1', 'key2' => 'value2');
Works like a charm!
Thanks!
The problem was that some how, wordpress was deleting the $_POST at some point and after I've moved up my code to the top of the file, the problem was resolved.
Thanks you all!
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",
Hi I'm trying to send post values using a PHP script to an external website and get the result. My code is as follows
$postdata = http_build_query(
array(
'drpservice' => 091,
'drpdirection' => 1,
'drpbusstop' => 18051
)
);
$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($url, false, $context);
echo $result;
However, it won't return the required results unless I can manage to send in the hidden _VIEWSTATE value.
Anyone able to help?
You need to first get the form page using a GET request, and then you can parse the page to get the _VIEWSTATE value using a regular expression.
Then when doing the POST pass the value you got for it.