duanpei8518 2015-09-21 20:55
浏览 51
已采纳

将变量传递给数组PHP中的值

I'm trying to create a variable and place it in an array. Like this:

    $ch = curl_init('https://apps.net-results.com/api/v2/rpc/server.php?Controller=Contact');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
    json_encode(
        array(
            'id' => uniqid(),
            'method' => 'getMultiple',
            'jsonrpc' => '2.0',
            'params' => array(
                'offset' => 0,
                'limit' => 50, // 10, 25, or 50
                'order_by' => 'contact_email_address', //'contact_email_address' or 'contact_id'
                'order_dir' => 'ASC', //'ASC' or 'DESC'
            )
        )
    )
);

$strResponse = curl_exec($ch);
$strCurlError = curl_error($ch);
if (!empty($strCurlError)) {
    //handle curl error
    echo "Curl Error<br />$strCurlError<br />";
} else {
    //check for bad user/pass
    if ($strResponse == "HTTP/1.0 401 Unauthorized: Your username name and/or password are invalid.") {
        //handle login error
        echo "Error<br />$strResponse<br />";
    } else {
        //successful call, check for api success or error
        $objResponse = json_decode($strResponse);
        if (property_exists($objResponse, 'error') && !is_null($objResponse->error)) {
            //handle error
            $intErrorCode = $objResponse->error->code;
            $strMessage = $objResponse->error->message;
            $strData = $objResponse->error->data;
            echo "Error<br />Code: $intErrorCode<br />Message: $strMessage<br />Data: $strData<br />";
        } else {
            //handle success
            //echo "Success<br />";
            $objResult = $objResponse->result;
            $intTotalRecords = $objResult->totalRecords;
            //echo "Total Records: $intTotalRecords<br />";
            $arrContacts = $objResult->results;
            //echo $arrContacts[0]->country;
            //echo $arrContacts[3]->last_name;
            //echo "<pre>";
            //print_r($arrContacts);
            //echo "<pre/>";

        }
    }
}

I'm not sure this is possible. I have tried doing different things such as creating a class, and placing the function in the class in the array, but it doesn't work. Can anyone give me the correct syntax of what I should try?

  • 写回答

2条回答 默认 最新

  • douqilin4296 2015-09-21 20:59
    关注

    If you pass params via post you should use http_build_query() and you can pass it as CURL post fields.

    You should set:

    $array = [
        'id'      => uniqid(),
        'method'  => 'getContactActivity',
        'jsonrpc' => '2.0'
    ];
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"http://example.site");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
    curl_exec($ch);
    curl_close($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示