Scott
Scott

Reputation: 13

curl CLI to curl PHP

I use the following command in some old scripts:

curl -Lk "https:www.example.com/stuff/api.php?"

I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.

What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.

I suspect something in the order of :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

// What goes here so that I just get the Location and nothing else?

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch); 

The goal being $response = the data from the api “OK=1&ect”

Thank you

Upvotes: 1

Views: 678

Answers (1)

netcoder
netcoder

Reputation: 67695

I'm a little confused by your comment:

// What goes here so that I just get the Location and nothing else?

Anyway, if you want to obtain the response body from the remote server, use:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);

If you want to get the headers in the response (i.e.: what your comment might be referring to):

curl_setopt($ch, CURLOPT_HEADER, 1);

If your problem is that there is a redirection between the initial call and the response, use:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Upvotes: 2

Related Questions