Jump to content

PHP Curl request on local server

- - - - -

  • Please log in to reply
1 reply to this topic

#1
totonex

totonex

    Learning Programmer

  • Members
  • PipPipPip
  • 82 posts
Hello everyone,

I am using the following code to do a post to localhost:


$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://localhost/response.php');

//response.php just spits out json_encode($_POST)

curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/html"));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, Array('data' => $xml));

$curl_result = curl_exec($ch);

if(curl_errno($ch)) {

	echo 'Curl error: '.curl_error($ch);

}

curl_close($ch);

echo $curl_result;

//Outputs []


As stated in the comment, nothing gets posted to the server. What am i doing wrong?:confused:
BTW: If i change Array('data'=>$xml) to simply $xml (which is a string constructed previously) it stops working altogether (just as if a syntax error occurred).
Thanks !

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I am not one hundred percent sure there is something wrong, can you modify response.php to write to a log file what it retrieves in the request? Something likely has gone wrong and nothing was returned.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users