I'm using Shopify API with PHP CURL POST Method to create a product. Kindly find the following code and the response. I got error "Not Found".
<?php
$products_array = array(
"product"=>array(
'title'=>'',
"title"=> "Burton Custom Freestlye 151",
"body_html"=> "<strong>Good snowboard!</strong>",
"vendor"=> "Burton",
"product_type"=> "Snowboard",
"published"=> false ,
"variants"=>array(
array(
"sku"=>"t_009",
"price"=>20.00,
"grams"=>200,
"taxable"=>false,
)
)
)
);
echo json_encode($products_array);
echo "<br />";
$url = "https://apikey:password#hostname/admin/products.json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($products_array));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec ($curl);
curl_close ($curl);
echo "<pre>";
print_r($response);
?>
The response of the request :
{"product":{"title":"Burton Custom Freestlye 151","body_html":"Good snowboard!<\/strong>","vendor":"Burton","product_type":"Snowboard","published":false,"variants":[{"sku":"t_009","price":20,"grams":200,"taxable":false}]}}
HTTP/1.1 404 Not Found
Server: nginx
Date: Sat, 01 Apr 2017 21:08:04 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block; report=/xss- report?source%5Baction%5D=create&source%5Bapp%5D=Shopify& source%5Bcontroller%5D=admin%2Fproducts&source%5Bsection%5D=admin&source%5Buuid%5D=5539f94e-776f-4672-ab2a-f340c0cf2ad1
X-Dc: ash,chi2
X-Request-ID: 5539f94e-776f-4672-ab2a-f340c0cf2ad1
{"errors":"Not Found"}
How can I fix that error ? and what does that mean ??
Double check that you are using the right .myshopify.com hostname for your store. You can see this (along with the API key and password) after creating a private app in the Shopify admin: https://shopify.com/admin/apps/private
Related
I get an error every time when I'm trying to merge a pull request with the bitbucket api.
This is my code:
define('USERNAME','***');
define('PASSWORD','***');
$url = "https://bitbucket.org/api/2.0/repositories/{owner}/{repo}/pullrequests/46/merge";
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl1, CURLOPT_USERPWD, USERNAME . ":" . PASSWORD);
curl_setopt($curl1, CURLOPT_HEADER, true);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_POST, true);
echo curl_exec($curl1);
And this is the error code:
HTTP/1.1 100 Continue HTTP/1.1 400 BAD REQUEST Server: nginx/1.6.2 Date: Wed, 20 May 2015 15:43:27 GMT Content-Type: application/json; charset=utf-8 Content-Length: 113 Connection: keep-alive X-Render-Time: 0.218739032745 Content-Language: de ETag: "2f0273bc2b819d7505bc14bf84d7e129" X-Request-Count: 227 X-Served-By: app19 Vary: Authorization, Accept-Language, Cookie X-Frame-Options: SAMEORIGIN X-Static-Version: 3b0c7aec39d3 X-Version: c288eef4a422 {"error": {"message": "'ascii' codec can't encode character u'\\xe4' in position 11: ordinal not in range(128)"}}
I've already tried to send the informations (owner, repo and request id) with "CURLOPT_POSTFIELDS". But I got the same error.
Can somebody help me?
This is my code:
$url = "https://bitbucket.org/api/2.0/repositories/***/***/pullrequests/35/merge";
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl1, CURLOPT_USERPWD, "***:***");
curl_setopt($curl1, CURLOPT_HEADER, true);
curl_setopt($curl1, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_URL, $url);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl1, CURLOPT_POST, true);
echo curl_exec($curl1);
Thats the response:
HTTP/1.1 400 BAD REQUEST Server: nginx/1.5.10 Date: Wed, 04 Mar 2015 06:03:15 GMT Content-Type: text/plain Content-Length: 11 Connection: keep-alive X-Served-By: app19 X-Render-Time: 0.0410010814667 Content-Language: de X-Static-Version: 572a80470390 Vary: Authorization, Accept-Language, Cookie X-Version: 1d224fb664b6 ETag: "825644f747baab2c00e420dbbc39e4b3" X-Request-Count: 27 X-Frame-Options: SAMEORIGIN Bad Request
Why does this not work? (For safety reasons i replaced some informations with ***)
You are missing the mandatory parameters for that particular endpoint which should be included in your request body.
According to the API, those mandatory parameters are owner, repo_slug and pull_request_id.
$request_body = array(
'owner' => 'account-name',
'repo_slug' => 'repo-name',
'pull_request_id' => 35
);
Because you specified application/json as your Content-Type, you need to json_encode the array from above:
curl_setopt($curl1, CURLOPT_POSTFIELDS, json_encode($request_body));
As a side note, you could use bitbucket-api library, which can help you to interact with Bitbucket API in a more easy way.
Accepting a pull request using that library, looks something like this:
$pull = new Bitbucket\API\Repositories\PullRequests();
// set your login credentials here
$pull->getClient()->addListener(
new \Bitbucket\API\Http\Listener\BasicAuthListener('username', 'password')
);
$pull->accept($account_name, $repo_slug, 35);
You can read more in the docs.
Disclaimer: I am the author of bitbucket-api library.
I'm trying to work off the Harvest php API example from the GitHub page.
Here is the code, but there is no output no matter what I try to do. If I use the Chrome Postman app I can retrieve data. I'm not sure what I'm doing wrong. Confidential information was removed. Any help is much appreciated.
<?php
$url = "https://company.harvestapp.com/?daily=";
$temp = getURL($url);
echo $temp;
function getURL($url) {
$credentials = "<email>:<password>";
$headers = array(
"Content-Type: application/xml",
"Accept: application/xml",
"Authorization-Basic: " . base64_encode($credentials)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "test app site");
$data = curl_exec($ch);
curl_close($ch);
if ($data !=false){
return $data;
}
else
return "no data";
}
?>
This is the error I get from the curl_error function:
HTTP/1.1 401 Unauthorized Server: nginx Date: Fri, 09 May 2014 22:59:27 GMT Content-Type: application/xml; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive Status: 401 Unauthorized Cache-Control: private, no-store, no-cache, max-age=0, must-revalidate X-Frame-Options: SAMEORIGIN X-Served-From: https://.harvestapp.com/ X-UA-Compatible: IE=Edge,chrome=1 Set-Cookie: _harvest_sess=; domain=.harvestapp.com; path=/; secure; HttpOnly X-Request-Id: e9d6eb74-5c73-430e-9854-ebf6f1c527c8 X-Runtime: 0.019223 You must be authenticated with Harvest to complete this request.
I am using Gcm for sending notifications and I have used curl for the same. Here is the script
$registrationId= array('adkbvkasdjb');
$headers=array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$sendData=array(
'msg'=>'any message');
$url= GCM_URL;
$details=array(
'registration_ids'=>$registrationId,
'time_to_live'=> 48*60*60,
'data'=>$sendData,
);
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($details));
$result= curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
// closing the curl connection
curl_close($ch);
Now i am getting the header of the response as
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Date: Thu, 16 Jan 2014 11:05:17 GMT
Expires: Thu, 16 Jan 2014 11:05:17 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
Since I am not very good with regex and i searched a lot about a method that could parse the header of the response but all in vain.
Question:-
So i just wanna know is there anything or any method that could parse the header response as an array ?
Note:- I found a little about http_parse_header() but it need pcre to be installed so i am not sure whether it is installed on my REAL SERVER :)
You don't need any Regex to do it. If you read the first comment of parse_http_headers documentation on php.net, there is a little implementation if pecl_http lib is not found.
Follow the link:
http://www.php.net/manual/en/function.http-parse-headers.php#112986
I'm not a php developer! I'm just trying to make php curl work on my localhost, talking to my REST service...
Note: I have a self-signed certificate and I suspect php curl goes to [http://localhost:8443/myapp/oauth/token] instead of [https://localhost:8443/myapp/oauth/token]
But I couldn't figure out why?
Here is my code below;
public function oauth2() {
echo 'Starting...<br/>';
$postargs = array(
'grant_type' => 'password',
'client_id' => 'some-client',
'client_secret' => 'some-secret',
'scope' => 'play,trust',
'username' => 'tester',
'password' => '121212'
);
$url = 'https://localhost:8443/myapp/oauth/token';
try {
$this->load->library('curl');
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_PORT, 8443);
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl_handle, CURLOPT_SSLVERSION, 3);
curl_setopt($curl_handle, CURLOPT_NOBODY, true);
//curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true);
//curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 10);
//curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $postargs);
$buffer = curl_exec($curl_handle);
if (curl_errno($curl_handle)) {
echo curl_error($curl_handle).'<br/>';
} else {
//echo curl_getinfo($curl_handle);
curl_close($curl_handle);
echo '<p>'.$buffer.'</p>';
}
} catch (Exception $e) {
echo 'Exception....<br/>';
echo $e->getMessage();
echo $e->getTraceAsString();
die;
}
echo 'Continue...<br/>';
die;
}
Above doesn't work... Why do I get "HTTP/1.1 100 Continue" in the beginning? It shouldn't be like that! Here is the response I see.
HTTP/1.1 100 Continue HTTP/1.1 401
Unauthorized Server: Apache-Coyote/1.1
Cache-Control: no-store
Pragma: no-cache
WWW-Authenticate: Basic realm="***/client",
error="unauthorized",
error_description="An Authentication object was not found in the SecurityContext"
Content-Type: application/json;
charset=UTF-8 Transfer-Encoding: chunked Date: Fri,
10 Jan 2014 04:44:40 GMT
{"error":"unauthorized","error_description":"An Authentication object was not found in the SecurityContext"}
However when I run curl from command line like below it works!
curl -k -i -H "Accept: application/json" -X POST -d "grant_type=password&client_id=some-client&client_secret=some-secret&scope=play,trust&username=tester&password=121212" [https://localhost:8443/myapp/oauth/token]
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Cache-Control: no-store
Pragma: no-cache
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 10 Jan 2014 04:56:45 GMT
{"access_token":"cee03c37-9168-4dca-8ffe-bd7f469cdcb5","token_type":"bearer","expires_in":5238,"scope":"play trust","client_id":"...."}
Found the problem!
Changing below line worked!
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "grant_type=password&client_id=....&client_secret=....&scope=play,trust&username=tester&password=121212");
Instead of an array it needed a string.