0
<?php
    $ch = curl_init("URL");
    $username = "USERNAME";
    $password = "PASSWORD";
    $fields = array("from"=>array("name"=>"Bob","city"=>"SAINT-LAURENT","country"=>"CA","state"=>"QC","postal_code"=>"H4R1W4"),"to"=>array("is_commercial"=>true,"city"=>"ANJOU","country"=>"CA","state"=>"QC","postal_code"=>"H1J1Z4"),"packages"=>array("units"=>"imperial","type"=>"package","items"=>array("width"=>1,"height"=>2,"length"=>3,"weight"=>4)));
    curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
    curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));        
    $data = curl_exec($ch);
    if ($data === FALSE) 
    { 
        $temp=curl_error($ch);
        echo "<p>cURL Error: {$temp}</p>";
    }
    echo "<pre>";
    print_r($data);
    curl_close($ch);
?>

Error I got is

  {"error": "Unexpected input. Please make sure this is a valid JSON document"}

I'm unable to understand what this meant .
After Update the error i get is
Argument 1 passed to Support\ExposedObjectAbstract::setFromArray() must be of the type array, integer given, called in Shipping/Packages.php on line 22 and defined

2
  • 1
    it means you need to send json on this url. this is back response from server it will auth with valid json so you need to send a json on that Commented Apr 16, 2014 at 6:54
  • trying this json_encode($fields) is giving me some response error Commented Apr 16, 2014 at 7:07

3 Answers 3

1

Your server end point (the URL you're calling) requires the input to be a valid JSON. In order to do this, you can replace:

$field_string = http_build_query($fields);

with:

$field_string = json_encode($fields);
Sign up to request clarification or add additional context in comments.

2 Comments

Works but gives a new error "Argument 1 passed to Support\ExposedObjectAbstract::setFromArray() must be of the type array"
This is the endpoint server side error. Consult the server owner.
1

First set the HTTP header that you are sending JSON

curl_setopt($ch,CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

And then, convert your array into JSON, and then setting to postfields

curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));

4 Comments

givces me the same error "Argument 1 passed to Support\ExposedObjectAbstract::setFromArray() must be of the type array"
@Harsha update your question with these suggestion. I mean update your code in the question.
Thanks for your suggestion. Updated question accordingly
Your code is OK. Its the problem with your JSON. They array you are using is not the correct one I must say. Please see their API doc and adjust your array according to your one.
0

$field_string = http_build_query($fields); is what the problem is , try to pass as array

curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.