I use this class to make my life easier, I have used it in pure PHP and in Laravel, you can try for Postman the service is fine and then move to the code
<?php
/**
* Created by PhpStorm.
* User: Alex Diaz
* WebServiceMangerCurl
*
*/
class WebServiceManagerCurl
{
private $url;
private $args;
private $proxy;
private $proxyIp;
private $proxyUser;
private $proxyPass;
/**
* WebServiceManagerCurl constructor.
* @param string $url
* @param Array $args Argumentos para enviar al Webservice
* @param bool $proxy
* @param int $proxyIp Ip con Puerto Ej: 190.0.0.1:8080
* @param string $proxyUser
* @param string $proxyPass
*/
public function __construct($url, $args = '', $proxy = false, $proxyIp = 0, $proxyUser = '', $proxyPass = '')
{
$this->url = $url;
$this->args = $args;
$this->proxy = $proxy;
if ($proxy) {
$this->proxyIp = $proxyIp;
$this->proxyUser = $proxyUser;
$this->proxyPass = $proxyPass;
}
}
public function get()
{
$curl = curl_init($this->url);
if ($this->proxy) {
curl_setopt($curl, CURLOPT_PROXY, $this->proxyIp); // PROXY details with port
$proxyUserPwd = $this->proxyUser . ':' . $this->proxyPass;
curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyUserPwd);
}
/**
* CURLOPT_RETURNTRANSFER
* TRUE para devolver el resultado de la transferencia como string
* del valor de curl_exec() en lugar de mostrarlo directamente.
* - tomado de php.net
**/
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('User-Agent: php-curl'));
/**
* CURLOPT_SSL_VERIFYPEER
* FALSE para que cURL no verifique el peer del certificado.
* Para usar diferentes certificados para la verificación se pueden especificar con la opción CURLOPT_CAINFO
* o se puede especificar el directorio donde se encuentra el certificado con la opción CURLOPT_CAPATH.
* - tomado de php.net
*
*/
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
/**
* curl_exec
* Ejecuta la sesión cURL que se le pasa como parámetro.
* - tomado de php.net
*/
$response = curl_exec($curl);
/**
* curl_getinfo
* Obtener información relativa a una transferencia específica
* - tomado de php.net
*/
$info = curl_getinfo($curl);
if ($info['http_code'] == 200) {
print_r($response);
print_r($info);
} else {
echo "Curl error: " . curl_error($curl);
}
curl_close($curl);
}
}
To try it you can start with a pure php so that you understand it, if you do not use a proxy, change it to false and do not send those parameters, I put them because I must use them.
Enter this page and generate a URL that receives your requests link will give you something like link
If it works for you here, your script is fine and you should check the url you use,
<?php
/**
* Created by PhpStorm.
* User: AlexDiaz
* Date: 15/09/2017
* Time: 8:17 AM
*/
include_once 'WebServices/WebServiceManagerCurl.php';
$webService = new WebServiceManagerCurl('https://requestb.in/11tqqii1', '', $proxy = true, $proxyIp = '177.777.777.7777:8781', $proxyUser = 'usuarioproxy', $proxyPass = 'claveproxy');
$webService->get();
I'll return something like:
okArray ( [url] => https://requestb.in/11tqqii1 [content_type] => text/html; charset=utf-8 [http_code] => 200 [header_size] => 519 [request_size] => 196 [filetime] => -1 [ssl_verify_result] => 20 [redirect_count] => 0 [total_time] => 1.922...
Now in laravel you can create a controller with this class that happens to you, add the route in the web.php (depends on the version use 5.4)