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

2
  • 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 Commented Feb 5, 2016 at 3: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. Commented Feb 5, 2016 at 4:24

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

5 Comments

'p6' => '{New York}',); is last comma need in last array after York?
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 if you are keen for more discussion of the topic.
Bingo, Ghost's CURLOPT_SSL_VERIFYPEER need to add , John's full code all works now. Just need un-comment CURLOPT_SSL_VERIFYPEER line
Nice work. Disabling SSL verification should be a temporary workaround, I recommend you look at this answer to get your script working in a secure fashion.
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);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.