T
T
TopClans2015-11-29 23:15:08
PHP
TopClans, 2015-11-29 23:15:08

Why is PHP variable value not being sent via POST?

You need to send a request to a site of the following type: site.ru/text?AAA=aaa&BBB=bbb&CCC=ccc, and before that, ask the user for these same aaa, bbb, ccc.
Learned through the form, recorded in the variables $a, $b, $c.
We made a common variable: $send = site.ru/text?AAA=$a&BBB=$b&CCC=$c It
remains to send this very variable using the POST or GET method to another site.
To be on the safe side, I display the value of the $send variable to check what we have collected from the forms. The value is always true, and if you copy it and paste it into the address bar, then the recipient will process this command.
It remains to try to force the script to send this data on its own.
I tried to do without cURL first, deducted the transfer method via sockets - did not work.
Then I decided to connect cURL, but still I get the same answer: the first parameter (AAA) is not specified. I suppose something cuts off everything after /?, because if you enter the URL just site.ru (without /text/) for interest, then the main page of the site starts in response.
Here is an example of sending a value (by the way, it is taken from a similar question on the toaster, and I tried a lot of other examples from other sites - nothing works).

$myCurl = curl_init();
curl_setopt_array($myCurl, array(
    CURLOPT_URL => 'http://site.ru/text/',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query(array($send))
));
$response = curl_exec($myCurl);
curl_close($myCurl);

echo "Ответ на Ваш запрос: ".$response;

Is there a way to output to myself and see what the script sends?

UPDATE:
I found out by experience that everything after the ? sign is cut off.
If you send a request to site.ru/text, then the answer will be: the parameter data is not specified. If you send not a variable, but the text site.ru/text?AAA=aaa, then the answer will be: the AAA parameter is not specified.
So now there is a new question: why is the second half of the request being cut off?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-11-30
@Denormalization

So you send "site.ru/text?AAA=$a&BBB=$b&CCC=$c" as POST parameters. Of course the server will swear.
You need to do this:

$send = array(
   'aaa' => $a,
   'bbb' => $b,
   'ccc' => $c
);
$myCurl = curl_init();
curl_setopt_array($myCurl, array(
    CURLOPT_URL => 'http://site.ru/text/',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query(array($send))
));
$response = curl_exec($myCurl);
curl_close($myCurl);

echo "Ответ на Ваш запрос: ".$response;

I will add:
CURLOPT_URL - here you specify the URL to which you need to send a request, everything is correct
here CURLOPT_POSTFIELDS - here you need to specify the BODY of the request, and you passed the entire URL here. The http_build_query function essentially just builds the array into the sequence "aaa=$a&bbb=$b" (roughly speaking)
Maybe you don't need the POST method? If "site.ru/text?AAA=$a&BBB=$b&CCC=$c" works correctly when pasted into your browser, then maybe you need a GET request? For GET you can just use file_get_contents($url); For example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question