0

I am trying to send HTTP data with CURL in PHP file , the code not send the data.

$url= 'http://www.example.com/test.php?' ;
$msg= 'p1=1234&p2=1234&p3=1234' ;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL,$url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $msg);

    curl_exec ($curl);
    curl_close ($curl);
Haim Rodrik
  • 61
  • 2
  • 6
  • What error do you get? What output do you expect? How do you know it isn't sending the data? – Ben Jan 25 '16 at 15:48
  • Not getting any error, just the string not sent – Haim Rodrik Jan 25 '16 at 15:52
  • There is tons of questions like this... For example [this](http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code?rq=1). – Smar Jan 25 '16 at 16:35

2 Answers2

0

You need to send post fields as an array, not as a 'GET' request string:

$msg = [
 'p1' => '1234',
 'p2' => '1234',
 'p3' => '1234',
];

If you want to send this as a one message you can send an array with one value:

$msg = ['msg' => 'some_message'];

If you want to send GET request, append your query string to the URL:

curl_setopt($curl, CURLOPT_URL, $url.$msg);

In the future you can get debugging info by using this code:

$result = curl_exec($curl);
echo 'curl_error: ';
var_dump(curl_error($curl));
echo '<br /><br /> curl_result: ';
var_dump($result);
curl_close($curl);
Alan
  • 1,234
  • 1
  • 15
  • 32
  • Thanks , you solved similar problem in other PHP file by making array. tried and works. Regarding the case here, is it possible to send as one message and not as array ? – Haim Rodrik Jan 25 '16 at 16:01
0

Please can you try the code below ?

$requestBody can be an array, querystring, json or else according to your request headers and the request handler which will recieve your http request.

contents of the test-curl.php:

<?PHP
error_reporting();
ini_set('display_errors', 'On');

function makeRequest($url, $requestBody)
{
    $handle = curl_init();
    $headers = array(); //array of request headers
    //Example headers for standart browser request
    //$headers = array(
    //   'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');

    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($handle, CURLOPT_POST, 1);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $requestBody);
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($handle, CURLOPT_HEADER, 1); //This means include headers in response

    $result = curl_exec($handle);
    $header_size = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
    $responseHeaders = substr($result, 0, $header_size);
    $responseBody = substr($result, $header_size, strlen($result) - $header_size);
    $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
    if ($httpCode == 200)
        return $responseBody;
    else
        throw new Exception($responseBody);
}

$method = $_SERVER["REQUEST_METHOD"];

if ($method == "GET") {

    $request = array();
    $request["a"] = "1";
    $request["b"] = "3";
    $request["c"] = "4";
    $request["d"] = "7";
    //$request = "a=1&b=3&c=4&d=7"; //This is the same with array version for standart post request.
    $response = makeRequest("http://localhost/test-curl.php", $request);
    echo $response;
} else {
    print_r($_POST);
}
?>

If you run your test-curl.php with http://localhost/test-curl.php url it will make a post request to itself and you will see the print_r output of $_POST array. Something like below;

Array ( [a] => 1 [b] => 3 [c] => 4 [d] => 7 )

Hope this helps you.

Cihan Uygun
  • 1,881
  • 1
  • 14
  • 22
  • I renamed $requestBody to $msg in order to keep versatility with variables, makerequest($url,$msg); got: PHP Fatal error: Uncaught exception 'Exception' in C:\Inetpub\www\example.com\httpdocs\test.php:84 Stack trace: #0 C:\Inetpub\www\example.com\httpdocs\test.php(59): makeRequest('http://www.example.com/test.php?', 'p1=1234&p2=1234&p3=1234') #1 {main} thrown inC:\Inetpub\www\example.com\httpdocs\test.php on line 84 Sağol ;) – Haim Rodrik Jan 25 '16 at 16:59
  • I have modified my answer in order to debug yourself. If method is thorwing an exception as you see the end of the makeRequest method the response code is not 200 which means OK. Please check my modified answer. And please notice that you must provide full url, not relative one. @user1570701 – Cihan Uygun Jan 25 '16 at 17:09
  • Cihan Bey, I am using real domain name while testing. Once, I wrote full domain name and some hacker started to pinging the server until server collapsed. Since then I am afraid to publish real domain names. I can send you the domain name privately not public. I am trying to make it works, I am sure that I am missing some silly point. – Haim Rodrik Jan 25 '16 at 23:59
  • Haim Bey, Thats ok you do not need to write down your exact domain name here. But my modified answer should give you an output even an error or exception message. What my updated answer's output ? @HaimRodrik – Cihan Uygun Jan 26 '16 at 07:04
  • I am guessing the problem. In your code have 2 variables, $requestBody and $headers My variables as below : $url= 'http://www.example.com/test.php?' ; $msg= 'p1=1234&p2=1234&p3=1234' ; Actually no need array , because $msg include all fields in one variable without need to covert to array. Do you think that need to use explode command and insert into array as in your example ? – Haim Rodrik Jan 26 '16 at 20:30
  • @HaimRodrik yes and $headers is a local variable and $requestBody is a parameter. If you try to run the code without modifying it you will see that its working. I have tested that code before answering to your question. – Cihan Uygun Jan 26 '16 at 20:45