How to test Curl PHP?

Asked

Viewed 3,444 times

0

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

Can anyone tell me why this occurs?

  • Put the code so we know if it’s a problem with him.

2 answers

3


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

Make sure it’s installed:

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

When setting the parameters put 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 of 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’ve helped. :)

  • Hello Paul. Curl check returns (1), but when I var_dump, nothing returns.

  • 1

    If it returns 1 (true) it means that Curl is installed. It tries to execute the above code by submitting another url that returns a json. Ex.: site.com.br/file.txt - Where this file is a simple json code. As you said you have access to both servers, try first on the same server as the requesting application and then on the remote server. Note: Make sure you are sending the values in the request exactly as the other site asks.

  • I did the test on the server where the code is and brought the result. This means that the problem may be on the other server?

  • This or the data the other server is asking for is not being sent correctly. After the line $data = curl_exec($ch); execute echo curl_error($ch); to see what error is happening. If there are any put here to investigate. By good practice at the end of all add the line curl_close($ch); to release the resources.

1

I don’t have much patience to use the functions of Curl php, because you end up having to type a lot.

My suggestion is to use the Guzzle, which is a PHP library, 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 launched. So you can see if there is something 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());
}
  • GuzzleHttp uses Curl for requests. It is only an abstraction power library and functionality. Recommending making use of it without even knowing how to use the basics of Curl can lead to confusion and lack of experience. But anyway, nice suggestion and examplo.

  • @juniorb2ss I don’t know if you share the same opinion, but I find the way to use Curl (for a simple test) more confusing than using a simple instance of Guzzle

  • Abstraction is more complete and easier, but my comment was the recommendation for someone who does not know the basics of Curl, understand? By the way, Guzzle is great and I use it frequently.

  • Yes I do. Thank you for your comments

Browser other questions tagged

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