Can't seem to find an error in my PHP code. I am trying to add a customer over Shopify API, below is my code. I tried the same URL using Postman and it worked - the user gets added when I add the same JSON string. So I know that API is enabled properly with correct permissions within Shopify.
$API_KEY = '****';
$PASS = '****';
$STORE_URL = '****';
$DATE = "2021-07";
$USER_EMAIL = 'test#test.com';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/api/'.$DATE.'/customers.json';
// post to: POST /admin/api/2021-07/customers.json
$data = array('customer' =>
array(
'first_name' => 'Promo',
'last_name' => 'User',
'email' => $USER_EMAIL,
'accepts_marketing' => 'true',
),
);
echo "test post data:";
echo json_encode($data);
$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 );
echo "test return data:";
var_dump($json);
The code prints the following, returning NULL and no customer is added:
test post data:{"customer":{"first_name":"Promo","last_name":"User","email":"test#test.com","accepts_marketing":"true"}}test return data:NULL
Can anyone spot what am I doing wrong here?
Never mind, I just figured it out.
The line
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
Should be
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
Related
I've tried changing things around and all I every get is 'false'.
(I was getting errors until I fixed a problem with the strlen) - now just false.
(I have no problem getting records to view)
I have tried this as PUT and PATCH...
$user = 'someuser';
$base_url = 'https://creator.zoho.com';
$url = "$base_url/api/v2/$user/client-list-2/report/Contact";
$ch = curl_init($url);
$criteria = "ID=98989231239213"; // id is not a string in zoho
$fields = json_encode(
[ "criteria" => $criteria,
"data" => ["RBT_ID" => "$rbt_id"] / RBT_ID is a string in zoho
]
);
$fields_length = strlen($fields);
$header = array('Content-Type: application/json',
"Content-Length: $fields_length",
"Authorization: Zoho-oauthtoken $creds->access_token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
curl_close($ch);
I need to create a local host using google client api . I wrote following code to crate local post.
$service = new Google_Service_MyBusiness($client);
$callToAction = new Google_Service_MyBusiness_CallToAction();
$callToAction->setActionType('LEARN_MORE');
$callToAction->setUrl('localhost.com');
$mediaItem = new Google_Service_MyBusiness_MediaItem();
$mediaItem->setMediaFormat('PHOTO');
$mediaItem->setSourceUrl('https://www.theanthem.com/images/usps_eddm_postcards.jpg');
$localPost = new Google_Service_MyBusiness_LocalPost();
$localPost->setLanguageCode('en-US');
$localPost->setSummary('Just a summary');
$localPost->setCallToAction($callToAction);
$localPost->setMedia($mediaItem);
$parent = sprintf('accounts/%d/locations/%d',
'116633170334837786295',
'8830395735149945322'
);
$service->accounts_locations_localPosts->create($parent, $localPost);
But Unfortunately I got following error.
My Error: Message: Error calling POST
https://mybusiness.googleapis.com/v4/accounts/9223372036854775807/locations/8830395735149945322/localPosts:
(400) Request contains an invalid argument.
How can i fix this?
I found the answer:
create refresh token
$request_url = "https://www.googleapis.com/oauth2/v4/token";
$refresh_token = "*** Refresf Token ***";
$params = [
'client_id' => "***your client id***",
'client_secret' => "***your clinet secret id***",
'refresh_token' => $refresh_token,
'grant_type' => "refresh_token"
];
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v) {
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$json_response = curl_exec($curl);
$response = (array) json_decode( $json_response );
$myaccess_token = $response['access_token'];
And create Post
$location = 'accounts/accoutid/locations/location id';
$api_end_point_url = 'https://mybusiness.googleapis.com/v4/'.$location.'/localPosts';
$postfields = array(
'topicType' => "STANDARD",
'languageCode' => "en_US",
'summary' => 'test post 123',
);
$data_string = json_encode($postfields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_end_point_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $myaccess_token,'Content-Type: application/json'));
$data1 = json_decode(curl_exec($ch), true);
$http_code1 = curl_getinfo($ch, CURLINFO_HTTP_CODE);
I am using the following code to initiate a member add request to a list in MailChimp.
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$email = 'test#domain.com';
$first_name = 'name';
$last_name = 'last name';
$api_key = 'xx-us18'; // YOUR API KEY
// server name followed by a dot.
// We use us13 because us13 is present in API KEY
$server = 'us18.';
$list_id = 'xx'; // YOUR LIST ID
$auth = base64_encode( 'user:'.$api_key );
$data = array(
'apikey' => $api_key,
'email_address' => $email,
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => $first_name,
'LNAME' => $last_name
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://'.$server.'api.mailchimp.com/3.0/lists/'.$list_id.'/members');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json',
'Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
echo $result;
?>
But what happened is the response is 404.
{
type: "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
title: "Resource Not Found",
status: 404,
detail: "The requested resource could not be found.",
instance: "78a35efa-ef43-471d-9e70-aecdc992d2e6"
}
Does anyone know what I am doing wrong? What might be the reason for this error?
Extra Infos: API Key is valid, If I provide that wrong, I am getting another error.
List-id is also true.
I am running this code on localhost with MAMP.
The issue was I used web_id instead of list_id.
When I changed that, the issue was resolved.
I've been stuck trying to update or delete a metafield that i created on user registration via the shopify customer API.
At first i tried to update the metafield using the customer API but it told me that the metafield already existed and i cannot add it again.
Then i used the user ID and made a call to retrieve all the metafields linked to the user using the metafield API - this worked and it returned all the metafields for me.
However my issue starts when i want to either delete or update the metafield that i've created.
I've tried a lot of different variations of REST calls but nothing seems to work.
To try and delete i used:
$baseUrl = "https://".$apikey.":".$password."#".$hostname."/admin/";
......
$curl = curl_init($baseUrl.'metafields/'.$metaID.'.json');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
// Make the REST call, returning the result
$response = curl_exec($curl);
var_dump($response);
This did not do anything, so i tried going a level deeper and used the following URL:
$curl = curl_init($baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json');
This URL did not work either.
Then I decided that i might as well just try and update the metafield to the new value that it was supposed to have.
I then tried the tried the following to update the value:
$metafield = array('metafields' => array(array(
'id' => $metaID,
'value' => '3',
'value_type' => 'string',
)));
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
$ch = curl_init($curl_url);
$data_string = json_encode(array('metafield'=>$metafield));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
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))
);
$server_output = curl_exec ($ch);
curl_close ($ch);
var_dump($server_output);
After that i tried the alternative URL:
$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
Any idea where i messed up?
Part 2 works now - creating and updating a metafield
So after writing and testing different sets of code i got something to work.
$data = array('metafield' =>
array(
'key' => 'level',
'value' => '1',
'value_type' => 'string',
'namespace' => 'Wholesaler'
)
);
$curl_url = $baseUrl.'customers/'.$custID.'/metafields.json';
//$ch = curl_init($baseUrl.$sid.'.json'); //set the url
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: POST') );
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
echo "<br>-------------------------------------------------<br>";
echo $curl_url;
This code allowed me to create a metafield for a user that did not have it as well as update that metafield, as long as the key field and namespace makes it unique it will create a new field, if you use a key and namespace that already exists it will update the value.
I hope this helps someone else out there.
I do this call and it works perfect everytime:
DELETE /admin/customers/123456789/metafields/987654321.json
Where my customer ID is 123456789 and the metafield I want to kill has the ID 987654321
Part one is done - Delete
After doing alot of research and trying different pieces of script the following worked for me:
$curl_url = $baseUrl.'customers/'.$custID.'/metafields/'.$metaID.'.json';
//$curl_url = $baseUrl.'metafields/'.$metaID.'.json';
$session = curl_init($curl_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if(ereg("^(https)",$curl_url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
I can now delete a metafield.
I need to send some data from my form in html to webservice. (For do this, i need to do the operation of POST)
I have seen research that I can transmit information using php cURL. But in all examples, i don't view to send data to webservice, only to file php that print $_POST variable.
I have this webservice: http://192.168.1.1/fastfood/event/attendee (example)
And i try to send data in an array.
For example i try to send: attendee = array( 'name' => $_POST['name'] , 'lastname' => $_POST['lastname'] , 'address' => $_POST['address'] );
Then, the web service takes out the array data. ¿How to do this?
UPDATE 1:
This is my code that i'm doing now... But don't work :(
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$attendee = array(
'name' => "$name",
'lastname' => "$lastname",
'address' => "$address"
);
$url_target = 'http://192.168.1.1/fastfood/event/attendee';
//$header = array('Content type: multipart/form-data');
$user = 'root';
$pass = '123';
$userpasswd = "$user:$pass";
$ch = curl_init($url_target);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
//curl_setopt($ch, CURLOPT_URL, $url_target);
//curl_setopt($ch, CURLOPT_HEADER, TRUE);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
$getInfo = curl_getinfo($ch);
curl_close($ch);
The variable $result return me a FALSE, and the variable $getInfo return me a http_code = 500, Content-Type = Null.
Reading the documentation of cURL when i send a data like a array, the content type should be a "multipart/form-data" but, also, don't work for me.
// Here is the data we will be sending to the service
$data = array(
'name' => $_POST['name'],
'lastname' => $_POST['last_name'],
'address' => $_POST['address']
);
$curl = curl_init('http://192.168.1.1/fastfood/event/attendee');
curl_setopt($curl, CURLOPT_POST, 1); //Choosing the POST method
curl_setopt($curl, CURLOPT_URL, 'http://localhost/helloservice.php'); // Set the url path we want to call
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Make it so the data coming back is put into a string
curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data); // Insert the data
// Send the request
$result = curl_exec($curl);
// Free up the resources $curl is using
curl_close($curl);
echo $result;
Written by Chad Lung at GiantFlyingSaucer