2012-04-10 90 views
1

我环顾了一下,找不到解决方案来解决我的问题。我收到以下错误:“请求的URL返回错误:413。”curl POST错误:请求的URL返回错误:413

我发布了一些通过curl和PHP通过HTTPS URL的信息。我被告知必须传递“Content-Length”以及我的请求,因为目标是https而不是http。

下面是我使用的代码:

$user = '11111111111111111'; 
    $books = array(111); 

    $data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books); 
    $data_string = array('json'=>json_encode($data)); 
    $target_url = 'https://www.test.com/test.php'; // fake URL of course 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_URL,$target_url);                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                   
     'Content-Type: application/json',                     
     'Content-Length: ' . strlen($data_string))          
    );                             

    $api = curl_exec($ch); 

我的strlen($ data_string)命令返回的值是5,这是比$ data_string的实际长度,这是更长的时间要少得多。我认为这是问题,除非有人认为这可能是由其他原因引起的。

+0

对数组调用的strlen()返回字符串“Array”的长度,因为数组不是字符串:] – orourkek 2012-04-10 23:34:09

+0

什么是适当的函数? count()只会返回数组中元素的数量,对吧? – Bill 2012-04-10 23:40:59

+0

我很困惑,为什么你不只是传递一个字符串,而不是一个元素的数组? – orourkek 2012-04-11 15:53:01

回答

1

这里是我结束了:

$user = '11111111111111111'; 
    $books = array(111); 

    $data = array("action" => "books-shared", "userID" => $user, "bookIDs" => $books); 
    $data_string = array('json'=>json_encode($data)); 

    $target_url = 'https://www.test.com/test.php'; // fake URL of course 

    $ch = curl_init(); 
    if (!$ch) { 
     die("Couldn't initialize a cURL handle"); 
    }  
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_VERBOSE, true);   
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
    curl_setopt($ch, CURLOPT_URL, $target_url);                 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FAILONERROR, true);                             

    $api = curl_exec($ch); 

无SSL或大小错误,它似乎很好地工作。

相关问题