Sam Majeed
Sam Majeed

Reputation: 1

Curl call to bigcommerce site with no response

I'm trying to make a curl call to a bigcommerce store locally, but I'm getting no response. The store is hosted in a different domain. When the api url is executed manually it works.

I checked for the curl_error and it seems to be the following error:

< HTTP/1.1 301 Moved Permanently..

This is the sample code

<?php
$api_url = 'site url/api/v2/blog/posts/count';
$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $api_url); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:application/json','Content-Length: 0'));                                   
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 ); 
curl_setopt( $ch, CURLOPT_USERPWD, "username:passwd" ); 
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); 
$response = curl_exec( $ch ); 

Upvotes: 0

Views: 79

Answers (1)

Victor T.
Victor T.

Reputation: 360

The error you get indicates that the document you want to access has moved, and the API responds with the new location to follow. Just tell cURL to follow this new location by setting the CURLOPT_FOLLOWLOCATION option to true:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

Upvotes: 2

Related Questions