POST Multiple POST Values

  //Setup the POST fields
  $PostData = array(
    'my_field1' => $my_field1,
    'my_field1' => $my_field1
  );
  $PostData = http_build_query($PostData);
  
  $SendUrl = 'https://urltosendto.com';
  
	if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
  {
		$ch =  curl_init();
      curl_setopt($ch, CURLOPT_URL, $SendUrl);
			curl_setopt($ch, CURLOPT_POST, True);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);			//Connect timeout in secs
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);				//Maximum time the request is allowed to take in secs
			$Response = curl_exec($ch);
		curl_close($ch);
	}

  if (strpos($Response, 'SUCCESS') !== False)
  {
    //It worked
  }

POST a json based request

  $PostData = array(
    'SomeField1' => $MyVariable,
    'SomeField1' => 1
  );
  $PostData = json_encode($PostData);

  $Url = "https://myurl.com";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $Url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));   //<<<You can add header fields in here. e.g.: curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', "Token:$MyAcccessToken"));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 		//Follow any page redirects etc (optional)
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);				//<<<Include this for the output of curl to go to the curl_exec() output below instead of the browser screen
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);			  //Connect timeout in secs
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);				      //Maximum time the request is allowed to take in secs
  $CurlResult = curl_exec($ch);
  curl_close($ch);

  $DecodedJson = json_decode($CurlResult, True);
  
  echo "CurlResult: ";
  print_r($CurlResult);
  echo "<br>DecodedJson: ";
  print_r($DecodedJson);
  
  if (!is_null($DecodedJson))
  {
    //Check for succcess
    if ( (isset($DecodedJson['errorCode'])) && ($DecodedJson['errorCode'] == 0) )
    {
    }
  }

POST a json block with authorisation

  $PostData = "    {
      \"messages\":[
          {
              \"source\":\"php\",
              \"body\":\"$MessageBody\",
              \"to\":\"$DestinationTelephoneNumber\",
              \"from\":\"" . LOGIN_SMS_OUR_TELEPHONE_NUMBER . "\"
          }
      ]
  }";
  
  $SendUrl = 'https://urltosendto.com';
  $Username = 'myusername';
  $Password = 'mypassword';
  
	if(function_exists('curl_init') && function_exists('curl_setopt') && function_exists('curl_exec'))
  {
		$ch =  curl_init();
      curl_setopt($ch, CURLOPT_URL, $SendUrl);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $PostData);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
			curl_setopt($ch, CURLOPT_TIMEOUT, 10);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$Username:$Password"), 'Content-Type: application/json'));
			$Response = curl_exec($ch);
		curl_close($ch);
	}

  //response_code":"SUCCESS"
  $Response = json_decode($Response, TRUE);
  $response_code = $Response['response_code'];
  
  if ($response_code == "SUCCESS")
  {
    
  }
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *