PHP to Service by CURL

Asked

Viewed 89 times

0

I’m trying to send PHP data through CURL to the Protheus service, which receives a string.

I’m having trouble referencing the data to the variable.

Example in PHP:

$response2 = '[{"A1_COD":"'.$row['CLI_PROTHEUS'].'",';
$response2 .= '"A1_PESSOA":"'.$row['CLI_PESSOA'].'",';
$response2 .= '"A1_CGC":"'.$row['CLI_CGC'].'",';
$response2 .= '"A1_NOME":"'. $row['CLI_NOME'].'",';

$url  = 'http://blabla/REST01/ADRESTCR1/putcli';
$ch   = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '**dadosCli**='.$response2);

$result = curl_exec($ch);
curl_close($ch);

The problem is to pass the parameter, the URL should be http://blabla/REST01/ADRESTCR1/putcli?dadosCli=VALORES

This is the problem.... if I send everything directly in the URL, it gives that conversion problem because of accents and spaces.

Does anyone have any light??? the problem is to pass the parameter that needs to receive the value.

  • You need to make a POST request and the fields will be sent by URL?

  • Exactly Anderson, pass the value to the "dataCli" parameter of the URL

1 answer

1


The function urlencode(string):string returns a string in which all non-alphanumeric characters except -, _ and , are replaced with a percent sign (%) followed by two hexadecimal digits and coded spaces as a (+).

<?php
$response2 = '[{"A1_COD":"'.$row['CLI_PROTHEUS'].'",';
$response2 .= '"A1_PESSOA":"'.$row['CLI_PESSOA'].'",';
$response2 .= '"A1_CGC":"'.$row['CLI_CGC'].'",';
$response2 .= '"A1_NOME":"'. $row['CLI_NOME'].'",';

$url  = 'http://blabla/REST01/ADRESTCR1/putcli';
$ch   = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, 1);

//Use urlencode() sobre $response2
curl_setopt($ch, CURLOPT_POSTFIELDS, 'dadosCli='.urlencode($response2) ); // Tirei os asteriscos pois julguei ser uma tentativa de destaque 

$result = curl_exec($ch);
curl_close($ch);

?>

If you are going to receive the value in an http request you do not have to worry about decoding the parameter with urldecode(string):string in the documentation is highlighted: that

The super-global $_GET and $_REQUEST They’re already being decoded. Use urldecode() in a $_GET or $_REQUEST element can generate unexpected and dangerous results.

Although it is not explicitly documented it extends to $POST

  • 1

    that’s right, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.