使用 PHP 和 Codeigniter 登录 API 失败



我尝试使用此代码登录到外部 API,但我没有得到任何响应(没有任何响应(。

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain/rest/');
curl_setopt($ch, CURLOPT_POST, 1);// set post data to true
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=MY_USERNAME&password=MY_PW");   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close ($ch);
var_dump($json);
die("Stopp");

我做错了什么?或者有没有更好/更简单的登录方法?

更新 与此同时,我已经将代码更改为这个

curl_setopt($ch, CURLOPT_URL, 'https://shop.mydomain.com/rest/');        
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"username=myapiuser&password=myapiuserpw");   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
$headers = curl_getinfo($ch);
$errors = curl_error($ch);
curl_close ($ch);
echo"<hr>";
var_dump($json);
echo"<hr>";
var_dump($headers);
echo"<hr>";
var_dump($ch);
echo"<hr>";
var_dump($errors);

我在调用函数时收到这个

字符串(0( " array(30( { ["url"]=> string(33( "https://shop.mydomain.com/rest/" ["content_type"]=> string(24( "text/html;charset=UTF-8" ["http_code"]=> int(200( ["header_size"]=> int(499( ["request_size"]=> int(170( ["filetime"]=> int(-1( ["ssl_verify_result"]=> int(0( ["redirect_count"]=> int(0( ["total_time"]=> float(0.( 275712( ["namelookup_time"]=> 浮点数(0.00649( ["connect_time"]=> 浮点数(0.009913( ["pretransfer_time"]=> 浮点数(0.035844( ["size_upload"]=> 浮点数(37( ["size_download"]=> 浮点数(0( ["speed_download"]=> 浮点数(0( ["speed_upload"]=> 浮点数(134( ["download_content_length"]=> 浮点数(-1( ["upload_content_length"]=> 浮点数(37

( ["starttransfer_time"]=> 浮点数(0.275399( ["redirect_time"]=> 浮点数(0( ["redirect_url"]=> 字符串(0( " ["primary_ip"]=> 字符串(13( "3.122.214.243" ["certinfo"]=> array(0( { } ["primary_port"]=> int(443( ["local_ip"]=> 字符串(13( "192.168.48.28" ["local_port"]=> int(54111( ["http_version"]=> int(2( ["protocol"]=> int(2( ["ssl_verifyresult"]=> int(0( ["scheme"]=> 字符串(5( "HTTPS" } 资源(6( 类型(未知( 字符串(0( "

尝试:

$data = array('username' => 'MY_USERNAME', 'password' => 'MY_PW');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

而不是:

curl_setopt($ch, CURLOPT_POSTFIELDS,"username=MY_USERNAME&password=MY_PW"); 

如果是客户端身份验证,请使用:

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");

我们需要更多信息来帮助您,什么API之王,... 不要忘记关闭帖子并接受答案,如果它解决了您的问题。

相关内容

  • 没有找到相关文章

最新更新