简体   繁体   中英

how to pass get method method in curl array

I'm using url to pass row data from one page to another

echo "<td >
    <a href='sessiondetails.php?docname={$key['DocName']}& HosName={$key['HosName']}& HosCode={$key['HosCode']}............}'>
       More>>
    </a></font>
    </td>
</tr>";

In my second page I am using $_GET to assign the values to variables and echoing is working good.
But when I assign the variable as array value it does not pass the value, I can't understand why.

Here is my second page code:

<html>
    <head>
        <link href='style1.css' rel='stylesheet' type='text/css'>
    <?php
        $SpecializationId = $_GET['SpecialitionID'];
        $DoctorNo         = $_GET['DoctorNo'] ;
        $day              = $_GET['day'];
        $date             = $_GET['date']; 
        $HosCode          = $_GET['HosCode'];       
        $hospital         = $_GET['HosName'];
        $doctor           = $_GET['docname'];
        $specialization   = $_GET['SpecName'];

        $baseurl = 'http://202.124.173.187/api/v1/doctorSessions';    
        $rawPOSTdata = array(
            "hosID"  => $HosCode,
            "specID" => $SpecializationId,
            "docNo"  => $DoctorNo,
            "day"    => $day,
            "date"   => $date 
        );

        $curl = curl_init($baseurl);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
             'Content-Type: application/json',
             "Authorization: Bearer $atoken"
        )); 
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($rawPOSTdata));    
        $response = curl_exec($curl);
        curl_close($curl);

        if($response) {
            if (isset($result->error) ) die( $result->error_message );
            /* Convert json data to array */

            $arr=json_decode( $response,true);
    ?>

"hosID"=>$hosCode, is not taking value

set POST via both CURLOPT_CUSTOMREQUEST and CURLOPT_POST and the CURLOPT_CUSTOMREQUEST persisted as POST while CURLOPT_POST switched to CURLOPT_HTTPGET. The Server assumed the header from CURLOPT_CUSTOMREQUEST to be the right one and came back with a 411.

curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');

you're not escaping the GET variables properly, nor are you encoding it for HTML properly, and you're probably getting a data corruption here. to escape it properly, use http_build_query to make your url, and html encode it with htmlentities or htmlspecialchars , like this

echo '<td >
    <a href="sessiondetails.php?'.htmlentities(http_build_query(array('docname'=>$key['DocName'],'HosName'=>$key['HosName'],'HosCode'=>$key['HosCode'])),ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE | ENT_DISALLOWED,'UTF-8',true).'">
       More>>
    </a></font>
    </td>
</tr>';

in the future, you can use https://validator.w3.org/ to detect when you're not properly encoding variables to HTML, and would probably have detected your error here

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