2

I have CURL script as below:

$url= 'https://www.test.com/test.php';
$msg=?p1={1250.feed}&p2={jt2221}&p3={1330}&p4={1234567890}&p5={2016-02-04 20:05:34}&p6={New York}; 

$url .= $msg;
$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);

the full string url and message , when I copy/past on Chrome web browser the remote PHP file receives well. When same url+message sent by PHP script not works. I guess the problem is first the remote domain is HTTPS , second seems curly brackets and space disturbs the CURL request.I tried urlencode($msg) function then got Error 404 . On success sent message , remote PHP returns {"Code":null,"Msg":"."} as ACK

Vitali
  • 95
  • 1
  • 2
  • 6
  • 1
    try to turn off the flag `CURLOPT_SSL_VERIFYPEER` to false, and add a browser agent as well `CURLOPT_USERAGENT`, just search it in the net you'll get both of those no problem – Kevin Feb 05 '16 at 03:17
  • I added those lines : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36'); I am getting now Bad Request HTTP Error 400. The request is badly formed. – Vitali Feb 05 '16 at 04:24

1 Answers1

2

If you are using urlencode, you'll just want to encode the values, not the whole query string. An efficient way to do this (and keep your query data in neat arrays) is with http_build_query:

$url= 'https://www.test.com/test.php?';

$data = array('p1' => '{1250.feed}',
              'p2' => '{jt2221}',
              'p3' => '{1330}',
              'p4' => '{1234567890}',
              'p5' => '{2016-02-04 20:05:34}',
              'p6' => '{New York}',);

$msg = http_build_query($data);

$url .= $msg;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$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);
John C
  • 8,223
  • 2
  • 36
  • 47
  • 'p6' => '{New York}',); is last comma need in last array after York? – Vitali Feb 05 '16 at 05:30
  • No, that is just a habit, it makes it easier if you add any more values but otherwise doesn't make any difference. [See this question](http://stackoverflow.com/q/2829581/628267) if you are keen for more discussion of the topic. – John C Feb 05 '16 at 05:53
  • Bingo, Ghost's CURLOPT_SSL_VERIFYPEER need to add , John's full code all works now. Just need un-comment CURLOPT_SSL_VERIFYPEER line – Vitali Feb 05 '16 at 06:56
  • Nice work. Disabling SSL verification should be a temporary workaround, I recommend you look at [this answer](http://stackoverflow.com/a/16495053/628267) to get your script working in a secure fashion. – John C Feb 05 '16 at 08:05
  • I got your advice, modified as below, and works curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); – Vitali Feb 05 '16 at 15:07