FormJSON PHP for CURL,

0

How to move from ...

  

file_get_contents ()

for CURL or Guzzle

$content = http_build_query(array(
'oid' => '00SH1000000ASZF',
'retURL' => 'https://www.site.com.br/', 
'first_name' => $nome,                                              
'last_name' => $sobrenome,
'cpf__c' => rmCaracter($f['cpf']),
'mobile' => rmCaracter($f['phone']),
'email' => $f['email'],                                             
'type__c' => 'HAB',
'Lead_Source' => 'Website Concessionaria',
'sub_source_media__c' => 'Site',                                  
'dealer_code_interest__c' => '137492',
'opt_in_email__c' => '1',
'opt_in_phone__c' => '1',                                               
'model_interest__c' => $f['model'],
));

$context = stream_context_create(array(
'http' => array(
'method'  => 'POST',
'content' => $content,
)
));                                             

$result = file_get_contents('https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8', null, $context);

I'm getting an error in $ result . ::

  

Warning:   file_get_contents ( link ...):   failed to open stream: HTTP request failed! HTTP / 1.1 400 TLS 1.1 or   higher required in   /home/storage/9/8e/3d/teste1124/public_html/tpl/site.php on line 124

I think it's the Solution because, if I put the url of this webservice in the action of a form and send the same data, I do not have the error, but the way I mounted my json, ai.

My Form + Json = link

  

Most hosts now block the furl_open parameter that   allows you to use file_get_contents () to load data from a URL   external. I think I can use CURL or a PHP client library   like Guzzle, as a solution.

    
asked by anonymous 17.11.2017 / 23:53

1 answer

0

Using cURL would look like this:

$content = array(
  'oid' => '00SH1000000ASZF',
  'retURL' => 'https://www.site.com.br/',
  'first_name' => $nome,
  'last_name' => $sobrenome,
  'cpf__c' => rmCaracter($f['cpf']),
  'mobile' => rmCaracter($f['phone']),
  'email' => $f['email'],
  'type__c' => 'HAB',
  'Lead_Source' => 'Website Concessionaria',
  'sub_source_media__c' => 'Site',
  'dealer_code_interest__c' => '137492',
  'opt_in_email__c' => '1',
  'opt_in_phone__c' => '1',
  'model_interest__c' => $f['model'],
);

$context = http_build_query($content);
$url = 'https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';

// Abre a conexão
$ch = curl_init();

// Seta a URL, método como POST, e os dados a ser enviado
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $context);

// Executa
$result = curl_exec($ch);
echo $result;

// Fecha a conexão
curl_close($ch);
  

Note: It was not possible to test because I am not currently in a suitable computer.

Reference

18.11.2017 / 00:27