I've been writing some php script ..
$gettarget = $argv[2];
echo "Trying to do something . . .\n";
sleep(2);
$ch = curl_init($gettarget); // initialize curl with given url
$useragent = "Some user agent ";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // add useragent
$response = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
echo $response;
echo "Done";
So here's what happens , It comes to " Trying to do something " and then nothing happens and the Done doesn't get echoed that means there's something wrong with curl , I don't really know what is up with cURL , So I thought I would post here.
You have not executed curl yet, in this code block. Need a $response = curl_exec( $ch ); at the end.
$gettarget = $argv[2];
echo "Trying to do something . . .\n";
sleep(2);
$ch = curl_init($gettarget); // initialize curl with given url
$useragent = "Some user agent ";
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); // add useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // setopt() does not exec()
// write the response to a variable
$response = curl_exec( $ch );
echo $response;
echo "Done";
$response = curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
should be
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
The curl_setopt() call tells it to return the response when you call curl_exec()
Related
I'm new to PHP and i'm trying to send this of to a webservice.
<?php
$myID = "8125987";
$myVarible = "This is a test";
$service_url = "https://...?password=123&user=123&storeid=1000&mobile=$myID&message=$myVarible";
$curl = curl_init($service_url);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>
It is not working, what is wrong?
Thanks for any help.
KHJ
1) Check if your curl function exists first
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 =
disabled.
2) Try to access example.com instead of your target url
$url = "http://www.example.com";
3) Print out the curl info and err message before you asked a question
There're lots of CURL examples on the internet, however, I still did one for your reference.
https://www.stockeasymoney.com/ns/sit/curlRaw.php
<?php
echo function_exists('curl_version') ? 1:0; // 1 = enabled , 0 = disabled.
echo "<pre>";
$url = "http://www.example.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
print_r( curl_getinfo($ch) );
curl_close($ch);
var_dump($result);
echo "</pre>";
?>
I'm trying to make a post request with php using curl however the json is not getting delivered to the REST API. Here is my code. In the webservice all I get is null value. I'm not sure where I'm going wrong.
$email_json_data = json_encode($email_data);
$header[] = "Content-type: application/json";
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $email_json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
return $response;
Webservice code:
$email_json_data = $this->post('email_json_data');
$email_data = json_decode($email_json_data);
Check PHP: curl_errno
There's probably a problem connecting to the server, and it's probably in one of your $header. To find out more, you need to show (in production, LOG it) the curl error.
In the future, please try to include a complete code sample, rather than just snippets
Code added from PHP: curl_strerror
class CurlAdapter
{
private $api_url = 'www.somewhere.com/api/server.php';
private $error = "";
private function jsonPost($data)
{
// init curl
$ch = curl_init($this->api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl header
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// build post data
$post_data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// execute
if (empty($response = curl_exec($ch)) {
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
$this->error = "cURL error ({$errno}):\n {$error_message}";
// #todo log curl error
}
}
// Close the handle
curl_close($ch);
return $response;
}
public function post( mixed $data )
{
if (empty($this->jsonPost($data))) {
return $this->error;
}
return $response;
}
}
$ca = new CurlAdapter();
echo $ca->post(['data' => 'testdata']);
Figured out a way to make this work.
Replaced $email_json_data = $this->post('email_json_data');
with $email_json_data = file_get_contents("php://input");
im sending request to java server
im using PHP Curl function
static ip our server not support
im getting blank response from server im try all most Curl methods but not getting solutions.
<?php
$headers = array(
'Content-Type: application/json; charset=UTF-8',
);
$p1="9503680548";
$p2="MR";
$p3="MA";
$p4="KL";
$p5="10";
$p6="pom123";
$p7="pom123456";
$p8="123568";
$url="http://domain/rest/apiCallForTxn/recharge?";
$url.="serviceNo=".$p1."&serviceType=".$p2."&serviceSubType=".$p3."&circleCode=".$p4."&amount=".$p5."&userCode=".$p6."&password=".$p7."&clientTransactionId=".$p8;
$ch = curl_init();
// Set the url, number of GET vars, GET data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute request
$result = curl_exec($ch);
// Close connection
curl_close($ch);
// get the result and parse to JSON
$obj = json_decode($result, true);
echo $obj['SERVICE_NO'];
echo "</br>";
echo $obj['AMOUNT'];
echo "</br>";
echo $obj['SERVICE_TYPE'];
echo "</br>";
echo $obj['TXN_ID'];
echo "</br>";
echo $obj['STATUS'];
?>
I am trying to post json data with cURL. The idea is: In case the url is not accessible ( for example failure of internet connection) keep trying to post the json while you succeed. It works but when I put it in while loop it executes only once. What am I doing wrong:
$jsonDataEncoded = json_encode($event);
echo $jsonDataEncoded;
echo "\n";
$send_failure = true;
while ($send_failure) {
$url = "a";// intentionally inaccessible url
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($jsonDataEncoded)));
$result = curl_exec($ch);
if(curl_errno($ch)){
throw new Exception(curl_error($ch));
} else {$send_failure = false;}
return $result;
}
You must print and check the value in $result using var_dump($result); .
If there is any connection error it returns the error code which you can check manual in IF condition. Hope this might help you.
My PHP code (free.php) on http://techmentry.com/free.php is
<?php
{
//Variables to POST
$access_token = "b34480a685e7d638d9ee3e53cXXXXX";
$message = "hi";
$send_to = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('access_token' => $access_token,
'message' => urlencode('hi'),
'send_to' => $send_to,)
);
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>
I am getting this error: THE MESSAGE WAS BLANK
This error means: "The message field was blank or was not properly URL encoded" (as told by my SMS gateway). But as you can see that my message field isn't blank.
I believe you can't send an array to CURLOPT_POSTFIELDS, you would need to replace the line with the following
curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$accesstoken."&message=".urlencode('hi')."&send_to=".$send_to);
I hope this solves it
Use http_build_query():
<?php
{
$postdata = array();
//Variables to POST
$postdata['access_token'] = "b34480a685e7d638d9ee3e53cXXXXX";
$postdata['message'] = "hi";
$postdata['send_to'] = "existing_contacts";
//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata) );
//Execute CURL command and return into variable $result
$result = curl_exec($ch);
//Do stuff
echo "$result";
}
?>