Last active
December 29, 2015 12:19
-
-
Save lyonsun/7669839 to your computer and use it in GitHub Desktop.
using Curl to post request to external api.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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