I have this data
$data = array('username' => 'myname',
'password' => 'mypass'
);
$json = json_encode($data);
How can i pass the json as a variable to a url that looks like this
'http://example.com/login/?data='
this is what i have tried and get '400: data not passed' error
$data = array('username' => 'myname',
'password' => 'mypass'
);
$data_string = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_URL, 'http://example.com/login/?data='); //also 'http://example.com/login/'
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . strlen($data_string)
));
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// Exec
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
Any data used in the query string should be URL-encoded.
$url = 'http://example.com/login/' . http_build_query([
'data' => json_encode([
'username' => 'myname',
'password' => 'mypass'
])
]);
http_build_query() will take an associative array of parameters, and apply all the proper encodings, building the whole query string for you.
The URL returned:
http://example.com/login/data=%7B%22username%22%3A%22myname%22%2C%22password%22%3A%22mypass%22%7D
This is possible to do this, you are pretty much there.
$data = array('username' => 'myname',
'password' => 'mypass'
);
$json = json_encode($data);
$url = 'http://example.com/login/?data=' . urlencode($json);
This will be a safe url that can do what ever you want with it.
Related
I retrieve the Oauth token in Trustpilot with this script
function get_accesstoken($tp_username,$tp_password,$api_key,$api_secret)
{
$url = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';
$payloadName = array(
'grant_type' => 'password',
'username' => $tp_username,
'password' => $tp_password
);
$payload = http_build_query($payloadName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$api_key:$api_secret");
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = json_decode(curl_exec($curl));
print_r($return);
curl_close($curl);
return $return->access_token;
}
now, how can I retrieve the token, verify is exist and use it?
thanks
$url = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
$key = "YOUR API KEY";
$secret = "YOUR API SECRET";
$username = 'YOUR LOGIN EMAIL';
$password = 'YOUR LOGIN PASSWORD';
$payload = array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
);
$authEncodedKeys = base64_encode($key.":".$secret);
$options = array(
'http' => array (
'header' => "Authorization: Basic ".$authEncodedKeys.
"Content-Type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($payload)
)
);
$context = stream_context_create($options);
$results = json_encode(file_get_contents($url, false, $context));
echo $results;
This works for me. Try it.
I wrote this code. I send data to $url = "https://upload.box.com/api/2.0/files/content", but i don't get a response. Maybe someone has also had this problem?
$absolutePath = 'home/my/path/uploads/media/ClientPDF/0001/01/c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf';
$json = json_encode([
'name' => 'c607bea86bdb42220bb49aac722b4a5eb44be3db.pdf,
'parent' => ['id' => 7479666489] //parent folder
]);
$fields = [
'attributes' => $json,
'file' => #$absolutePath
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer MY_TOKEN_KEY'
'Content-Type:multipart/form-data'
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($ch);
var_dump(json_decode($response, true)); die;
var_dump - print nothing, i can't debug this request. Please, help me to solve this problem
I have written a php code that should print the response of a json call. But I am getting NULL output. I have checked my json call is working fine from a restclient.
Here is my code
<?php
$username = "user";
$password = "password";
$postData = array(
'username' => $username,
'password' => $password,
'msisdn' => "111111111"
);
$ch = curl_init('http://ip:<port>/xxx/yyy/zzz');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
var_dump($responseData);
?>
Thanks in advance.
Try:
$responseData = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid JSON.');
}
okay so i have solved my problem and re-wrote the code. I was actually not providing the correct form of username and password in the json fields which is really a silly mistake.
I am giving my updated code here
`
$data = array("user" => "$username", "pass" => "$password", "msisdn" => "$msisdn");
$data_string = json_encode($data);
$ch = curl_init('http://ip:port/call_center/account/general');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
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))
);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
echo $result;
?>`
Thanks everyone.
I am trying to use this code to insert item in youtubeplaylist and its not working as i want ... i think its showing me if playlist contain this id .
please check and let me know what i need to change in code to insert this ID in my playlist .
Here is code :
$option = array(
'part' => 'snippet',
'mine' => 'true',
'key' => 'AIzaSyCJlbT_sqTG2nOUMYU24WzzxXpOelaiYTA',
'access_token' => $_SESSION['info']['access_token'],
'playlistId' => $ID,
'kind' => 'youtube#video',
'videoId' => 'KMGuyGY5gvY',
);
$url = "https://www.googleapis.com/youtube/v3/playlistItems?".http_build_query($option, 'a', '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = "Authorization: Bearer " . $_SESSION['info']['access_token'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
$json_response = curl_exec($curl);
curl_close($curl);
$responseObj = json_decode($json_response);
thanks in advance.
I'm trying to post from a PHP page to Shopify to indicate that an order item has been fulfilled. I must not be doing this correctly because I keep getting a null result. I'm doing this on a test order that was cancelled, but I would expect that I'd at least get some sort of non-null response if that were the problem. Here is my code:
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.
'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$data = array('fulfillment' =>
array(
'tracking_number' => $TRACKING_NUMBER,
'tracking_company' => 'USPS',
'tracking_url' => $TRACKING_URL,
'line_items' =>
array(
'id'=>$ITEM_ID,
),
),
);
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
You are sending a json in the POST section, are you sure this is what you really want to do? The post data should be more like this, sending data1=test1&data2=test2. source
<?php
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_URL, $baseUrl);
curl_setopt($session, CURLOPT_POST, count($fields));
curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
If you really wants to send JSON via the POST fields, maybe you are missing the length of your data : source
curl_setopt($, , CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
Shouldn't you be using complete.json instead ??? I may be misunderstanding but it sounds like your trying to complete a pending fulfillment.
/admin/orders/#{id}/fulfillments/#{id}/complete.json
https://docs.shopify.com/api/fulfillment#complete
I found the problem. Shopify requires the line items to have an additional array, so instead of this:
'line_items' =>
array(
'id'=>$ITEM_ID,
)
It should be this:
'line_items' =>
array(
array(
'id'=>$ITEM_ID,
)
)