简体   繁体   中英

curl request with headers in PHP

I want to make a cURL request to a URL along with the following headers :

'Content-Type: application/json', 'Authorization': 'Basic XXXXXXXXXX'

I have the following code :

<?php

$post_url = "https://api.interlinkexpress.com/user/?action=login";

$curl = curl_init($post_url);

$headers = array(
        'Content-Type: application/json',
        'Authorization': 'Basic XXXXXXXXX'
        );
//curl_setopt($curl, CURLOPT_USERPWD, "username":"Password");
curl_setopt($curl, CURLOPT_URL, $post_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS,json_encode($post_data) );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
$post_response = curl_exec($curl); 

?>

it is showing the result as :

Client sent a Bad Request

which is the 'Authorization...' line under the $header array

Any suggestions/help is appreciated.

problem is in header.It needs to be fixed.authorization will e whole string like this:

$headers = array(
    'Content-Type: application/json',
    'Authorization: Basic XXXXXXXXX'
    );

let me give you a full example of curl:

 function CurlSendPostRequest($url,$request)
    {
        $authentication = base64_encode("username:password");

        $ch = curl_init($url);
        $options = array(
                CURLOPT_RETURNTRANSFER => true,         // return web page
                CURLOPT_HEADER         => false,        // don't return headers
                CURLOPT_FOLLOWLOCATION => false,         // follow redirects
               // CURLOPT_ENCODING       => "utf-8",           // handle all encodings
                CURLOPT_AUTOREFERER    => true,         // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 20,          // timeout on connect
                CURLOPT_TIMEOUT        => 20,          // timeout on response
                CURLOPT_POST            => 1,            // i am sending post data
                CURLOPT_POSTFIELDS     => $request,    // this are my post vars
                CURLOPT_SSL_VERIFYHOST => 0,            // don't verify ssl
                CURLOPT_SSL_VERIFYPEER => false,        //
                CURLOPT_VERBOSE        => 1,
                CURLOPT_HTTPHEADER     => array(
                    "Authorization: Basic $authentication",
                    "Content-Type: application/json"
                )

        );

        curl_setopt_array($ch,$options);
        $data = curl_exec($ch);
        $curl_errno = curl_errno($ch);
        $curl_error = curl_error($ch);
        //echo $curl_errno;
        //echo $curl_error;
        curl_close($ch);
        return $data;
    }

500 Error typically suggests that the error is on the server side. You can probably try to emulate your request ( I would personally recommend using Advanced ReST Client , which is a google-chrome app), to verify whether this is the case.

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