Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to receive the data being posted using the curl commands. Data is URLify in order to build http query. The commands for data being sent using curl are :

    $data=array(
    'product_name' =>'Television',
    'price' => 1000,
    'quantity' => 10,
    'seller' =>'XYZ Traders'
  );
$DT=http_build_query($data);
 echo($DT);
$url = 'http://localhost/API2/products';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$DT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_json = curl_exec($ch);
echo $response_json;
curl_close($ch);

Now how to unpack the values being sent using curl build query. I am trying to get the values sent from curl like this but i don't get the values posted rather it gives me 1's and zero's:

    $_product_name=$_POST["product_name"];
    $_price=$_POST["price"];
    $_quantity=$_POST["quantity"];
    $_seller=$_POST["seller"];
    echo"data:= "+$_product_name+$_price+$_quantity+$_seller;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
268 views
Welcome To Ask or Share your Answers For Others

1 Answer

Change:

echo"data:= "+$_product_name+$_price+$_quantity+$_seller;

To:

echo "data:= " . $_product_name . $_price . $_quantity . $_seller;

Why? If you use + with strings in PHP, they get interpreted as 0 or 1 and added that way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...