Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:07:15+00:00 2026-06-14T05:07:15+00:00

I have the following curl command that works perfectly from the windows command line:

  • 0

I have the following curl command that works perfectly from the windows command line:

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X PUT -d   "{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"somebuy@bla.com\"}"   http://localhost:8080/users

So the jSon for this post is:

{\"name\":\"Name\",\"password\":\"1234\",\"email\":\"somebuy@bla.com\"}

I try to execute it using the following PHP Code:

$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT']; 
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_PORT, $port); //Set the port number
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // times out after 5s
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!empty($request)) {
  curl_setopt($ch, CURLOPT_POST, true);   
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields
}

if($port==443) {
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
}

$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
$error = curl_errno($ch);
curl_close($ch);

if ($error) {
  $error_codes = $this->_get_curl_error_codes();
  echo
    'Sorry, but we are currently not able to connect to the database. Error code '.$error.': '.$error_codes[$error];
  die();
}

I use this code as follows:

$url = 'http://localhost/users';
$port = 8080;
$request = array();
$request['name']        = 'Name';
$request['password']    = '1234';
$request['email']       = 'somebuy@bla.com';

Json is created like so:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request)); // add POST fields

My problem is that if I run it from the command line, it works, but if I run it using the code above, it doesn’t work. Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:07:17+00:00Added an answer on June 14, 2026 at 5:07 am

    curl command from console is using PUT, you are using POST

    try next options

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
    

    example http://www.lornajane.net/posts/2009/putting-data-fields-with-php-curl

    if it doesn’t work, also add headers

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

    One more thing:
    there is a $this->_get_curl_error_codes(); but I didn’t notice such method in your code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sending the following curl command from terminal in Mac OSX: curl -d
I have the following PHP code that uses cURL: $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,http://area51.stackexchange.com/users/flair/31.json); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
I have following script that executes all the .reg files in the current directory.
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
We have an MVC (MVC4) application which at times might get a JSON events
I have the following code which i copied from here : /*************************************************************************** * _
Am using curl exe in windows, to communicate with my Django backend. Following is
Assume I have a file at http://mysite.com/myscript.sh that contains: #!/bin/bash echo Hello $1 From
I have a Windows/Apache2/PHP app that receives file using chunked encoding. The reason is
I use the following command in some old scripts: curl -Lk https:www.example.com/stuff/api.php? I then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.