【问题标题】:How I will write this curl request我将如何编写这个 curl 请求
【发布时间】:2023-03-08 02:16:01
【问题描述】:

您好,我正在使用 API。在 API 文档中清楚地写道,aAll 数据以 JSON 格式发送和接收,采用 UTF-8 编码。然后他们给了一行

$ curl -­-­user name:password https://api.abc.de/erer

我只是想问一下我将如何发送上面提到的 curl 请求?用户名和密码将以GETPOSTheaders 发送?

我正在使用以下代码,但接收到空数组。文档说它必须收到一些错误或成功代码。

$ch = curl_init();
            $post_data = array('username'=>$_POST['username'],'password'=>$_POST['password']);
            $post_data = http_build_query($post_data);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, 'username:password');

            $result = curl_exec($ch);
            curl_close($ch);

            $result = json_decode($result);
            $result = (array) $result;
            echo "<pre>";
            print_r($result);
            echo "</pre>";
            die();

我已经打印出curl_get_info的回复

Array
(
[url] => https://api.abc.de/erer
[content_type] => 
[http_code] => 0
[header_size] => 0
[request_size] => 0
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0
[namelookup_time] => 0.618462
[connect_time] => 0
[pretransfer_time] => 0
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 0
[upload_content_length] => 0
[starttransfer_time] => 0
[redirect_time] => 0
)

【问题讨论】:

    标签: php json curl


    【解决方案1】:

    鉴于提供的信息,假设您正在发出 POST 请求,这样的事情应该可以工作:

    $post_data = http_build_query( $post_array );
    $url = 'https://api.abc.de/erer';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, 'name:password');
    $data = curl_exec( );
    curl_close($ch);
    

    $post_data 应该包含要发送的 key => 值的关联数组。 我相信你的用户:密码是在标题中发送的,但 PHP.net 没有说。

    http://www.php.net/manual/en/function.curl-setopt.php

    【讨论】:

    • L 你好,我已经更新了我的问题。请检查并告诉我。
    【解决方案2】:

    看看给定的例子..

    $token = "username:password";
    $url = "https://api.abc.de/erer";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_USERPWD, $token);
    /*curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);*/                    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    if( curl_exec($ch) === false ){
        echo curl_error($ch);
    }else{
        $data = curl_exec($ch);
    }
    curl_close($ch);
    
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    

    【讨论】:

    • 你好我已经更新了我的问题。请检查并告诉我。
    • @AwaisQarni 你想用 curl 向你的 API 发送什么?只有用户名:密码或其他字段??
    • @AwaisQarni 也使用 curl_error($ch); 来帮助您检测确切的错误。
    • 也可以试试 $response = curl_getinfo($ch); print_r($repsonse);然后发布你从中得到的东西和 curl_error
    • @jogesh_p curl_error 的响应是couldn't connect to host。这意味着服务器没有运行,我的代码没有问题。是吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 2012-01-21
    • 1970-01-01
    相关资源
    最近更新 更多