skip to Main Content

I have a problem with login in a API Software with PHP cURL.

//define variable
$utentesl = ...
$passwordsl = ...
$Appkey = ...

    header("CONTENT-TYPE: application/json;charset=utf-8");
       header("Access-Control-Allow-Origin: *");

        define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
 
        define('COOKIE_FILE', 'cookie.txt');
    

  
        curl_setopt($ch, CURLOPT_URL, $paginalogin);
  
        $accesso="Username=".$utentesl."&Password=". $passwordsl."&Appkey=".$appkey;
       


      
        curl_setopt($ch, CURLOPT_POST, 1);
      
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        
    
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
        
        //login
        curl_setopt($ch, CURLOPT_POSTFIELDS, $accesso);
        //non funziona ma esiste: curl_setopt($ch, CURLOPT_USERPWD, $accesso);


        //eseguo Login.
        $login = curl_exec($ch);
        $loginid = json_decode($login);
        
        
        //stampo risultato se necessario
        var_dump($loginid);

This code return:

object(stdClass)#2 (6) {
["StatusCode"]=>
string(20) "UnsupportedMediaType"
["ResponseCode"]=>
string(6) "02-001"
["ResponseId"]=>
int(1)
["ResponseIdDescription"]=>
string(21) "HttpResponseException"
["Message"]=>
string(22) "Unsupported Media Type"
["Details"]=>
NULL
}

I need to get from app a tocken ID.

I tried to insert various cURL variables (allow – origin: * , …) but still the same result.

If I add this code I get error:

"HTTP Error 400. The request is badly formed."

curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json','charset: utf-8',  'Accept: application/json', 'Content-Length: 10000']);

2

Answers


  1. Chosen as BEST ANSWER

    I Add this code to fix error:

    variables:

    $accesso = array('UserName' => $utentesl, 'Password' => $passwordsl , 'AppKey' => $appkey);
    $accesso = json_encode($accesso);
    

    And Headers before curl_exec :

    $ Headers = [      'Content-Type: Application/Json',      'Access-Control-ALLOW-ORIGIN: *'];    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    

  2. With no means of testing this particular API and not knowing the URL for the login endpoint might mean that the following is invalid but I hope that it might be useful.

    $utentesl = 'xxx';
    $passwordsl = 'xxx';
    $Appkey = 'xxx';
    
    # This is useful to ensure valid SSL certificates in HTTP connections!
    $cacert='/path/to/your/cacert.pem';
    /*
        You can download a copy of cacert.pem from
        https://curl.haxx.se/docs/caextract.html
    */
    
    # Simple utility to print out supplied data in readable format
    # will accept strings, arrays or objects as $data
    function debugprint($data=false,$msg='Success'){
        printf(
            '<h1>%2$s</h1><pre>%1$s</pre>',
            print_r($data,true),
            $msg
        );
    }   
    
    
    # Parameters to send in the POST body. You want to send JSON payload?!
    $args=array(
        'UserName'  =>  $utentesl,
        'Password'  =>  $passwordsl,
        'Appkey'    =>  $appkey
    );
    
    # Create the JSON payload using above array.
    $body=json_encode( $args );
    
    # Tell the server you are sending and expecting JSON
    # "Access-Control-Allow-Origin: *" is a Response header!
    $headers=array(
        'Content-Type'  =>  'application/json',
        'Accept'        =>  'application/json'
    );
    
    curl_setopt($ch, CURLOPT_URL, $paginalogin );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1)' );
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body );
    
    # If the url is SSL enabled make sure you set appropriate options!
    if( parse_url( $paginalogin, PHP_URL_SCHEME )=='https' ){
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_CAINFO, $cacert );
    }
    # Add the headers
    if( !empty( $headers ) && is_array( $headers ) ){
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
    # Make the request and...
    $result = curl_exec( $ch );
    
    # Capture the HTTP response code & info
    $code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
    $info = curl_getinfo( $ch );
    
    # Process response or debug request
    if( $result && $code==200 ){
        $json = json_decode( $result );
        debugprint( $json, 'Happy Happy! Joy Joy!' );
    }else{
        debugprint( $info, 'Request failed - why?' );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search