How to test PHP curl? [closed]

0

I have a project hosted on one server, however I plan to do an integration to another site using curl. But when passing the values from our database, it gives an error in this site that I want to do integration, saying that the data is incorrect. This site is from our group, but it is on another server. I was informed that the problem might be that PHP is not making the request for Curl, others say that the problem is in Curl, but when I take a look at info.php, the curl is active. Have some way to test CURL.

Does anyone know why this occurs?

    
asked by anonymous 03.05.2016 / 21:15

2 answers

2

It would be better if you showed the part of the code that is giving error. But the common mistakes when using CURL is that it is not installed on the server, the way it handles data and the SSL configuration. You can try the following:

Verify that it is installed:

function _isCurl() {
    return function_exists('curl_version');
}

In the parameter setting set 0 (zero) to SSL:

$ch = curl_init("URL_PARA_CONECTAR");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
...

Treat the result as a json array:

...
$data = curl_exec($ch);
$response = json_decode($data, true); // o true indica que você quer o resultado como array

An example configuration would be:

$valoresParaSubmeter = array('key1' => 'valor1', 'key2' => 'valor2');
$ch = curl_init("URL_PARA_CONECTAR");

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $valoresParaSubmeter);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
$response = json_decode($data, true); // o true indica que você quer o resultado como array

var_dump($response);

I hope I have helped. :)

    
04.05.2016 / 01:56
1

I do not have much patience to use the functions of Curl of php, since you end up having to type a lot.

My suggestion is to use Guzzle , which is a library in PHP, which is a client for making HTTP requests.

You can install it by Composer .

Then I would use a simple request:

$client = new GuzzleHttp\Client();

$res = $client->get('https://u.github.com/user', [
    'params' => ['id' => 1]
]);

If the request has no errors, you can capture the response with a echo $res->getBody() .

If the request fails, an Exception will be thrown. So you can see if something is wrong with the requests, or even capture them.

try{

    $rest = $client->get('https://non-exists');

} catch (\GuzzleHttp\Exception\RequestException $e) {

        var_dump($e->getResponse()->getBody());
}
    
04.05.2016 / 02:09