0

我创建了一个 cURL php 文件并将其包含在我的 index.php 文件中。部分代码显示在页面顶部。我不是最好的 PHP 编码器,但我相信它编码正确。这是PHP文件的代码:

<?php

//array of data to be posted
$post_data = array(
   'FirstName' => urlencode($firstname),
   'LastName' => urlencode($lastname),
   'UserIdentifier' => urlencode($regemail),
   'Password' => urlencode($regpass),
   'ReferrerAttribution' => '[referrer here]'
);
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//final string using implode()
$post_string = implode ('&', $post_items);

//create cURL connect
$curl_connection =
  curl_init('https://integration.[domain here].com/v1/[folder here]');

//set options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl_connection, CURLOPT_HEADER, true);

//set header
$header = array (
        'POST https://integration.[domain here].com/v1/[folder here] HTTP/1.1\r\n',
        'Content-Type: application/x-www-form-urlencoded\r\n',
        'Authorization: Basic "[auth key here]"\r\n'
 );
//set post data
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform request
$result = curl_exec($curl_connection);

//show request info
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
                curl_error($curl_connection);

//close
curl_close($curl_connection);

?>

这是页面顶部的错误:

Warning: curl_setopt(): supplied argument is not a valid cURL handle resource in /home/[main]/public_html/[folder]/signup.php on line 24
    Array ( [url] => https://integration.[domain].com/v1/[folder] [content_type] => text/html [http_code] => 401 [header_size] => 381 [request_size] => 285 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.294219 [namelookup_time] => 0.002752 [connect_time] => 0.081426 [pretransfer_time] => 0.26445 [size_upload] => 74 [size_download] => 58 [speed_download] => 25 [speed_upload] => 32 [download_content_length] => -1 [upload_content_length] => 74 [starttransfer_time] => 0.345831 [redirect_time] => 0 ) 0-

[] 括号用于显示我无法公开的信息。感谢您提前提供任何帮助。哦,这是我的第一篇文章。

4

1 回答 1

1

欢迎来到 StackOverflow!在以下代码行中,您传递了一个尚未定义的无效参数 $ch。将其更改为 $curl_connection。

曾是:

curl_setopt($ch, CURLOPT_POST, 1);

应该:

curl_setopt($curl_connection, CURLOPT_POST, 1);

编辑:

您也从未将您定义的标头应用于您的请求。你应该做这个:

curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $header);
于 2013-05-17T18:12:19.057 回答