Curl Bad Request

vikasbapat
vikasbapat used Ask the Experts™
on
We are getting error while using adding curl request in loop as shown in below code.
eventhough first entry from the loop gets consumed properly , from 2nd loop onwards we get error..

#$data is large XML file, which we divide in chunks and pass in for loop where curl request is added.

$posting1 = "xmldata=" . rawurlencode($data);
$Medi_URL2 = "https://proda.mediemr.net:4443/pls/htmldb/MEDIDATA.GET_SURESCRIPT_DATA_NEW";
$i = 0;
$len = 32000;
$arr2 = array();

while($i < strlen($posting1)) {
    $arr2[] = substr($posting1, $i, $len);
    $i += $len;
}


for($cnt=0;$cnt<count($arr2);$cnt++)
{
      $value=      $arr2[$cnt];
        $ch = curl_init();
      
      curl_setopt($ch, CURLOPT_URL,$Medi_URL2);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$value);
   

 ### Return post result string, no just success code
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


  $result = curl_exec ($ch);
  curl_close ($ch);
   unset($ch);
  unset($value);
}
BadRequestError.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
F P
Top Expert 2015

Commented:
Try a foreach loop instead of a for loop which counts an array. It's faster and more flexible. Lemme know if this works for you:

foreach($arr2 as $value)
{
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL,$Medi_URL2);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$value);

 ### Return post result string, no just success code
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec ($ch);
    curl_close ($ch);
}

unset($value);

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
From the look of the BadRequestError, the problem may be in the information you're sending to the API.  You can get error messages from cURL, just in case the problem is in cURL and not in the data itself.  This script shows how to do that.

Just curious - why did you decide to break the data into chunks?  POST-method requests typically can handle large amounts of data.  Is there any API documentation that recommends breaking the data into chunks?

<?php // demo/curl_post_example.php

/**
 * Demonstrate how to use cURL to make a POST request
 * This may be used to start an asynchronous process
 *
 * http://php.net/manual/en/book.curl.php
 */
error_reporting(E_ALL);


function curl_post($url, $post_array=array(), $timeout=2, $error_report=TRUE)
{
    // PREPARE THE POST STRING
    $post_string = NULL;
    foreach ($post_array as $key => $val)
    {
        $post_string .= $key . '=' . urlencode($val) . '&';
    }
    $post_string = rtrim($post_string, '&');

    // PREPARE THE CURL CALL
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL,            $url           );
    curl_setopt( $curl, CURLOPT_HEADER,         FALSE          );
    curl_setopt( $curl, CURLOPT_POST,           TRUE           );
    curl_setopt( $curl, CURLOPT_POSTFIELDS,     $post_string   );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate' );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout       );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE           );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE           );
    curl_setopt( $curl, CURLOPT_FAILONERROR,    TRUE           );

    // EXECUTE THE CURL CALL
    $htm = curl_exec($curl);
    $err = curl_errno($curl);
    $inf = curl_getinfo($curl);

    // ON FAILURE
    if (!$htm)
    {
        // PROCESS ERRORS HERE
        if ($error_report)
        {
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            echo "<pre>\n";
            var_dump($inf);
            echo "</pre>\n";
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS
    curl_close($curl);
    return $htm;
}


// USAGE EXAMPLE CREATES ASSOCIATIVE ARRAY OF KEY=>VALUE PAIRS
$args["name"] = 'Ray';
$args["mail"] = 'Ray.Paseur@Gmail.com';

// ACTIVATE THIS TO SEE THE ARRAY OF ARGS
// var_dump($args);

// SET THE URL
$url = "http://Iconoun.com/demo/bounce_post.php";

// CALL CURL TO POST THE DATA
$htm = curl_post($url, $args, 3, TRUE);

// SHOW WHAT CAME BACK, IF ANYTHING
if ($htm)
{
    echo "<pre>";
    echo htmlentities($htm);
}
else
{
    echo "NO RESPONSE YET FROM $url -- MAYBE BECAUSE IT IS RUNNING ASYNCHRONOUSLY";
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial