i would like to post json data to remote url, and the url will return json format data. here is my code:
$post_array=Array(
"trips"=>array(
"departure_code"=> "SIN",
"arrival_code"=> "HKT",
"outbound_date"=>"2014-02-29",
"inbound_date"=> "2014-03-05"
),
"adults_count"=> 1
);
$content = json_encode($post_array);
$curl = curl_init($search_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
//$response = json_decode($result);
var_dump($result);
curl_close($curl);
The results should be:
{
"id": "pNQFapkhRQ6ZYxql4MQDbQ",
"key": "[SIN:HKT:2014-01-29:2014-02-05]~1~0~XX~FI",
"trips": [
{
"id": "SIN:HKT:2014-01-29:2014-02-05",
"departure_code": "SIN",
"departure_name": "Singapore",
"trip_type": "standard"
}
],
"cabin": "economy",
"adults_count": 1,
}
I got string(35) "{"message":"Problems parsing JSON"}" error. Thanks for help.
what is the $search_url ?
Do you have any documentation for this service ?
Try to change this line:
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
to:
curl_setopt(
$curl,
CURLOPT_HTTPHEADER,
array(
"Content-type: application/json",
"Content-Length: ".strlen($content)
)
);
Here I'm showing you a simple demo of the cURL
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:'.strlen($data_string)
));
$json_response = curl_exec($curl);
$curl_errorno = curl_errno($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
Related
I am using the following code:
<?php
$url = "https://api.challonge.com/v1/tournaments/hamiltonjenn/matches/245771359.json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"match":{"player1_votes": 4}}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
var_dump shows only bool(false)
Any ideas why this is happening? I cannot determine the reason this won't work.
I'm not sure why, but changing the curl_setopt($curl, CURLOPT_PUT, true); to curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); solved the issue. I'm gathering that CURLOPT_PUT, true is expecting a file, whereas CURLOPT_CUSTOMREQUEST, "PUT" is expecting an update to the string.
When I try to post data with bearer token on https://reqbin.com there is no error. But I copy the code:
<?php
$url = "https://api.real-debrid.com/rest/1.0/unrestrict/link";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer ZWP53ALWWN7*******GZHE66Z6BL54FV4Z2DWNS22URJUX6Q";
$headers["Content-Type"] = "text/plain";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = "link=https://rapidgator.net/file/6e3e93b5c15700c574b4313de8ad416f/";
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
and try to run on my servers I get this error.
string(43) "{ "error": "bad_token", "error_code": 8 }"
I m try to update some data on mongodb using cms api
on terminal i can update information like this
curl -X PUT -d name=12345a https://api1.MYWEBISITE.com/api/v1in/user/2039/?t=mytoken
now using PHP i have try so many ways and no one looks to work for me
tried like this
class Curl {
public function put($url, $data_string){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
return $result;
}
}
$curl = new Curl;
$data_string = '{"name" : "name123"}';
$url = "https://api1.MYWEBSITE.com/api/v1in/user/2039/?t=mytoken";
echo $curl->put($url, $data_string);
also i tried like this
$data = array( "name" => '12344');
$url = "https://api1.mywebsite.com/api/v1in/user/2039/?t=mytoken";
$curl = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$response = json_decode($result);
var_dump($response);
curl_close($curl);
both php solutions dont works, nothing is updated, no error msg is showing
any help?
Error
{"message":"Unexpected token '","body":"{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}"}
Code
$data = "{'type':'A','name':'tarunDhiman','data':'166.62.81.221','ttl':3600}";
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{token}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
The issue is solved by the help of #Slaiv206 And below the working code.
$data = '[{ "type":"A", "name":"tarunDhiman", "data":"255.255.255.0", "ttl":3600 }]';
$url = "https://api.godaddy.com/v1/domains/{domain}/records";
$headers = array(
'Content-Type: application/json',
'Accept : application/json',
'Authorization : sso-key {key}:{secret}' );
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($curl);
curl_close($curl);
print $response ;
exit;
I think the header Authorization string is:
Authorization: sso-key {KEY}:{SECRET}
and not:
Authorization : sso-key {key}:{token}
and in the json string use double quotes instead single quotes:
'{ "type":"A", "name":"tarunDhiman", "data":"166.62.81.221", "ttl":3600 }'
Simple question
Where would I put "Authorization: Bearer cajwune0fnr78ynj2kz6p8bad"
$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
Thanks!
You'd put it into:
***old
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Authorization: Bearer cajwune0fnr78ynj2kz6p8bad');
***new
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer cajwune0fnr78ynj2kz6p8bad'));