简体   繁体   中英

How can I connect to a API service using curl in php?

I'm trying to connect to a API service using the following php:

$url = 'https://api.wlvpn.com/v2/customers&api-key=my-api-key'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, "http://127.0.0.1/");
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);

print_r($output);
print_r($curl_error);

when I run it I get the following error:

couldn't connect to host

However, when I run the following command from my command line in ubuntu:

jai@ubuntu:/opt/lampp$  curl -u api-key:my-api-key https://api.wlvpn.com/v2/customers

I get a response as expected

Can anyone help me what I am missing here I think I am missing -u option but I dont have any idea how to put it on my php code

Here is your expected answer. The url isn't correct, because you're using & instead of ?. And then you're telling cURL to connect to a proxy on 127.0.0.1 (there is none, usually). And the ssl certificate is self-signed, so you have to set CURLOPT_SSL_VERIFYHOST and CURLOPT_SSL_VERIFYPEER to 0 and false.

This script works:

<?php
$url = 'https://api.wlvpn.com/v2/customers?api-key=my-api-key';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
$curl_error = curl_error($ch);
curl_close($ch);

print_r($output);
print_r($curl_error);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM