-1

Please look the codes as below :

$url='https://www.test.com/test.php';
$post='?field1=1&field2=2&filed3'; // no need array text as is
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_exec ($ch);
curl_close ($ch)

Should be simple code, I used as reference http://curl.haxx.se/libcurl/php/examples/simplepost.html

I modified the code by replacing to variables.

I need to send data to remote server which belong to third party. Other side's server have data base. When I copy manually the www.test.com/test.php?field1=1&field2=2&filed3 into web browser's then the data saved into data base at other server and having respond {"Code":15,"Msg":null"} on browser screen, that's mean data sent properly. When trying to send by PHP script, the data not save in remote data base also not getting respond message.

Vitali
  • 95
  • 1
  • 2
  • 6

1 Answers1

0

If the request works when you enter it into a web browser, chances are it is a GET rather than a POST request. A simple example of doing that would be as follows:

$url= 'http://www.test.com/test.php?field1=1&field2=2&filed3';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

var_dump($http_status);
var_dump($result);

I've also set the CURLOPT_RETURNTRANSFER option so you can capture the response in $result.

John C
  • 8,223
  • 2
  • 36
  • 47
  • You right, I checked, is GET request. I used your code still not working. var_dump($result) shows on screen bool(false) – Vitali Feb 03 '16 at 05:53
  • I've added some error handling to my answer so you can see if there is something going wrong. – John C Feb 03 '16 at 11:18
  • Tried , no error, got bool(false) message. Same string on web browser works well. – Vitali Feb 03 '16 at 14:19
  • the url is https , maybe that makes the problem ? I am adding on url the https string, maybe need another parameter – Vitali Feb 03 '16 at 19:31
  • I've added a check of the HTTP status code to my answer - beyond that you might want to have a look at [this answer](http://stackoverflow.com/a/14436877/628267) on how to do a more in depth debug of cURL. – John C Feb 03 '16 at 22:18
  • My friend remark maybe the curly brackets makes the problem , the real data here as is : $msg=?p1={1250.feed}&p2={jt2221}&p3={1330}&p4={1234567890}&p5={2016-02-04 20:05:34}; – Vitali Feb 04 '16 at 20:14
  • That format could well cause issues - you may want to use [`urlencode`](http://php.net/urlencode) to properly encode the values. I'm not sure how that string relates to the full URL, so not sure if you need to do each value or the whole thing. – John C Feb 04 '16 at 22:26
  • It might be better at this point to open a new question about how to appropriately encode the query with some more details - we've solved how to connect and the comments aren't a great place to work out this problem. – John C Feb 04 '16 at 23:41
  • As per your recommendation I opened new thread http://stackoverflow.com/questions/35215672/get-request-via-curl-in-php – Vitali Feb 05 '16 at 03:12