Skip to content

Instantly share code, notes, and snippets.

@lyonsun
Last active December 29, 2015 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lyonsun/7669839 to your computer and use it in GitHub Desktop.
Save lyonsun/7669839 to your computer and use it in GitHub Desktop.
using Curl to post request to external api.
<?php
// here is where you are going to post curl.
$test_url = "www.example.com";
// this is the content to be post.
$request_xml_content = "<request>this is a sample xml request</request>";
// init Curl call.
$ch = curl_init();
// set options for Curl.
// don't pass any header params.
curl_setopt($ch, CURLOPT_HEADER, 0);
// do return what response.
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// disable SSL verification.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// url to pass this curl POST.
curl_setopt($ch, CURLOPT_URL, $test_url);
// we are POSTing what we request.
curl_setopt($ch, CURLOPT_POST, 1);
// set request variable name as 'request'.
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=".$request_xml_content);
if(curl_exec($ch) === false)
{
return 'Curl error: ' . curl_error($ch);
}
// set time limit to 60 seconds to extend execute time.
$seconds = 60;
set_time_limit ($seconds);
// execute Curl.
$content=curl_exec($ch);
// return response.
return $content;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment